From 0cce22f8c16d68dd9ac5b2fce9bcc5e279131071 Mon Sep 17 00:00:00 2001 From: Ruslan Talpa Date: Fri, 20 Nov 2015 16:07:20 +0200 Subject: [PATCH] check request payload for structure & remove bad test for csv --- src/PostgREST/App.hs | 29 +++++++++-------------------- test/Feature/InsertSpec.hs | 7 ++++--- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 0836f3211..77ae8bb50 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -254,25 +254,14 @@ formatGeneralError message details = cs $ encode $ object [ "message" .= message, "details" .= details] --- parseRequestBody :: Bool -> RequestBody -> Either Text ([Text],[[Value]]) --- parseRequestBody isCsv reqBody = first cs $ --- checkStructure =<< --- if isCsv --- then do --- rows <- (map V.toList . V.toList) <$> CSV.decode CSV.NoHeader reqBody --- if null rows then Left "CSV requires header" -- TODO! should check if length rows > 1 (header and 1 row) --- else Right (head rows, (map $ map $ parseCsvCell . cs) (tail rows)) --- else eitherDecode reqBody >>= convertJson --- where --- checkStructure :: ([Text], [[Value]]) -> Either String ([Text], [[Value]]) --- checkStructure v --- | headerMatchesContent v = Right v --- | isCsv = Left "CSV header does not match rows length" --- | otherwise = Left "The number of keys in objects do not match" --- --- headerMatchesContent :: ([Text], [[Value]]) -> Bool --- headerMatchesContent (header, vals) = all ( (headerLength ==) . length) vals --- where headerLength = length header +checkStructure :: ([Text], [[Value]]) -> Either Text ([Text], [[Value]]) +checkStructure v + | headerMatchesContent v = Right v + | otherwise = Left "The number of keys in objects do not match" + +headerMatchesContent :: ([Text], [[Value]]) -> Bool +headerMatchesContent (header, vals) = all ( (headerLength ==) . length) vals + where headerLength = length header convertJson :: Value -> Either Text ([Text],[[Value]]) convertJson v = (,) <$> (header <$> normalized) <*> (vals <$> normalized) @@ -356,7 +345,7 @@ buildMutateApiRequest intent = payload = case iPayload intent of Just (PayloadJSON v) -> JSON.Array v _ -> undefined --TODO! fix - parsedBody = convertJson payload -- TODO! either check structure or refactor to send json directly to postgres + parsedBody = checkStructure =<< convertJson payload isSingleRecord = either (const False) ((==1) . length . snd ) parsedBody flds = join $ first formatParserError . mapM (parseField . cs) <$> (fst <$> parsedBody) vals = snd <$> parsedBody diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index 861a286ed..160bf5c1d 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -211,9 +211,10 @@ spec = afterAll_ resetDb $ around withApp $ do it "fails for too few" $ do p <- request methodPost "/no_pk" [("Content-Type", "text/csv")] "a,b\nfoo,bar\nbaz" liftIO $ simpleStatus p `shouldBe` badRequest400 - it "fails for too many" $ do - p <- request methodPost "/no_pk" [("Content-Type", "text/csv")] "a,b\nfoo,bar\nbaz,bat,bad" - liftIO $ simpleStatus p `shouldBe` badRequest400 + -- it does not fail because the extra columns are ignored + -- it "fails for too many" $ do + -- p <- request methodPost "/no_pk" [("Content-Type", "text/csv")] "a,b\nfoo,bar\nbaz,bat,bad" + -- liftIO $ simpleStatus p `shouldBe` badRequest400 describe "Putting record" $ do