Correct order endpoints
This commit is contained in:
parent
88ae856195
commit
33df90eb96
3 changed files with 72 additions and 52 deletions
|
@ -1090,8 +1090,12 @@ routes pipe config = do
|
||||||
])
|
])
|
||||||
--Get all closed orders for the address
|
--Get all closed orders for the address
|
||||||
get "/api/allorders" $ do
|
get "/api/allorders" $ do
|
||||||
addr <- param "address"
|
session <- param "session"
|
||||||
myOrders <- liftAndCatchIO $ run (findAllOrders addr)
|
user <- liftAndCatchIO $ run (findUser session)
|
||||||
|
case cast' . Doc =<< user of
|
||||||
|
Nothing -> status unauthorized401
|
||||||
|
Just u -> do
|
||||||
|
myOrders <- liftAndCatchIO $ run (findAllOrders $ uaddress u)
|
||||||
case myOrders of
|
case myOrders of
|
||||||
[] -> status noContent204
|
[] -> status noContent204
|
||||||
_ -> do
|
_ -> do
|
||||||
|
@ -1105,23 +1109,23 @@ routes pipe config = do
|
||||||
--Get order by id for receipts
|
--Get order by id for receipts
|
||||||
get "/order/:id" $ do
|
get "/order/:id" $ do
|
||||||
oId <- param "id"
|
oId <- param "id"
|
||||||
|
token <- param "token"
|
||||||
let r = mkRegex "^[a-f0-9]{24}$"
|
let r = mkRegex "^[a-f0-9]{24}$"
|
||||||
if matchTest r oId
|
if matchTest r oId
|
||||||
then do
|
then do
|
||||||
myOrder <- liftAndCatchIO $ run (findOrderById oId)
|
myOrder <- liftAndCatchIO $ run (findOrderById oId)
|
||||||
case myOrder of
|
case cast' . Doc =<< myOrder of
|
||||||
Nothing -> status noContent204
|
Nothing -> status noContent204
|
||||||
Just o -> do
|
|
||||||
let o' = cast' (Doc o)
|
|
||||||
case o' of
|
|
||||||
Nothing -> status internalServerError500
|
|
||||||
Just pOrder -> do
|
Just pOrder -> do
|
||||||
|
if qtoken pOrder == token
|
||||||
|
then do
|
||||||
status ok200
|
status ok200
|
||||||
Web.Scotty.json
|
Web.Scotty.json
|
||||||
(object
|
(object
|
||||||
[ "message" .= ("Order found!" :: String)
|
[ "message" .= ("Order found!" :: String)
|
||||||
, "order" .= toJSON (pOrder :: ZGoOrder)
|
, "order" .= toJSON (pOrder :: ZGoOrder)
|
||||||
])
|
])
|
||||||
|
else status forbidden403
|
||||||
else status badRequest400
|
else status badRequest400
|
||||||
--Get order by session
|
--Get order by session
|
||||||
get "/api/order" $ do
|
get "/api/order" $ do
|
||||||
|
@ -1186,8 +1190,16 @@ routes pipe config = do
|
||||||
--Delete order
|
--Delete order
|
||||||
Web.Scotty.delete "/api/order/:id" $ do
|
Web.Scotty.delete "/api/order/:id" $ do
|
||||||
oId <- param "id"
|
oId <- param "id"
|
||||||
|
session <- param "session"
|
||||||
|
o <- liftAndCatchIO $ run (findOrderById oId)
|
||||||
|
case cast' . Doc =<< o of
|
||||||
|
Nothing -> status badRequest400
|
||||||
|
Just order -> do
|
||||||
|
if qsession order == session
|
||||||
|
then do
|
||||||
liftAndCatchIO $ run (deleteOrder oId)
|
liftAndCatchIO $ run (deleteOrder oId)
|
||||||
status ok200
|
status ok200
|
||||||
|
else status forbidden403
|
||||||
-- Get language for component
|
-- Get language for component
|
||||||
get "/getmainlang" $ do
|
get "/getmainlang" $ do
|
||||||
lang <- param "lang"
|
lang <- param "lang"
|
||||||
|
@ -1599,6 +1611,6 @@ checkUser run s = do
|
||||||
generateToken :: IO String
|
generateToken :: IO String
|
||||||
generateToken = do
|
generateToken = do
|
||||||
rngState <- newCryptoRNGState
|
rngState <- newCryptoRNGState
|
||||||
runCryptoRNGT rngState $ randomString 16 "abcdef0123456789"
|
runCryptoRNGT rngState $ randomString 24 "abcdef0123456789"
|
||||||
|
|
||||||
debug = flip trace
|
debug = flip trace
|
||||||
|
|
|
@ -31,7 +31,7 @@ packages:
|
||||||
hackage: crypto-rng-0.3.0.1@sha256:04f4ae75943ecad8b794950985054130f272d65a588b6b6528f56df0bfedc4dc,1565
|
hackage: crypto-rng-0.3.0.1@sha256:04f4ae75943ecad8b794950985054130f272d65a588b6b6528f56df0bfedc4dc,1565
|
||||||
snapshots:
|
snapshots:
|
||||||
- completed:
|
- completed:
|
||||||
sha256: 42f77c84b34f68c30c2cd0bf8c349f617a0f428264362426290847a6a2019b64
|
sha256: 4c972e067bae16b95961dbfdd12e07f1ee6c8fffabbfa05c3d65100b03f548b7
|
||||||
size: 649618
|
size: 650253
|
||||||
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/19.yaml
|
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/23.yaml
|
||||||
original: lts-20.19
|
original: lts-20.23
|
||||||
|
|
62
test/Spec.hs
62
test/Spec.hs
|
@ -346,6 +346,7 @@ main = do
|
||||||
False
|
False
|
||||||
""
|
""
|
||||||
""
|
""
|
||||||
|
"testToken4321"
|
||||||
req <-
|
req <-
|
||||||
testPostJson "/api/order" $
|
testPostJson "/api/order" $
|
||||||
A.object ["payload" A..= A.toJSON testOrder]
|
A.object ["payload" A..= A.toJSON testOrder]
|
||||||
|
@ -372,6 +373,7 @@ main = do
|
||||||
False
|
False
|
||||||
""
|
""
|
||||||
""
|
""
|
||||||
|
"testToken4321"
|
||||||
req <-
|
req <-
|
||||||
testPostJson "/api/order" $
|
testPostJson "/api/order" $
|
||||||
A.object ["payload" A..= A.toJSON testOrder]
|
A.object ["payload" A..= A.toJSON testOrder]
|
||||||
|
@ -398,6 +400,7 @@ main = do
|
||||||
False
|
False
|
||||||
""
|
""
|
||||||
""
|
""
|
||||||
|
"testToken4321"
|
||||||
req <-
|
req <-
|
||||||
testPostJson "/api/order" $
|
testPostJson "/api/order" $
|
||||||
A.object ["payload" A..= A.toJSON testOrder]
|
A.object ["payload" A..= A.toJSON testOrder]
|
||||||
|
@ -422,48 +425,34 @@ main = do
|
||||||
res <- httpLBS req
|
res <- httpLBS req
|
||||||
getResponseStatus res `shouldBe` unauthorized401
|
getResponseStatus res `shouldBe` unauthorized401
|
||||||
it "get order by id" $ do
|
it "get order by id" $ do
|
||||||
req <- testGet "/order/627ab3ea2b05a76be3000000" []
|
req <-
|
||||||
|
testGet
|
||||||
|
"/order/627ab3ea2b05a76be3000000"
|
||||||
|
[("token", Just "testToken1234")]
|
||||||
res <- httpJSON req
|
res <- httpJSON req
|
||||||
getResponseStatus (res :: Response A.Value) `shouldBe` ok200
|
getResponseStatus (res :: Response A.Value) `shouldBe` ok200
|
||||||
it "get order with invalid id fails with 400" $ do
|
it "get order with invalid id fails with 400" $ do
|
||||||
req <- testGet "/order/6273hrb" []
|
req <- testGet "/order/6273hrb" [("token", Just "testToken1234")]
|
||||||
res <- httpLBS req
|
res <- httpLBS req
|
||||||
getResponseStatus res `shouldBe` badRequest400
|
getResponseStatus res `shouldBe` badRequest400
|
||||||
it "get order by id fails with bad token" $ do
|
it "get order by id fails with bad token" $ do
|
||||||
req <- testGet "/order/627ab3ea2b05a76be3000000" []
|
req <-
|
||||||
|
testGet
|
||||||
|
"/order/627ab3ea2b05a76be3000000"
|
||||||
|
[("token", Just "wrongToken1234")]
|
||||||
res <- httpLBS req
|
res <- httpLBS req
|
||||||
getResponseStatus res `shouldBe` unauthorized401
|
getResponseStatus res `shouldBe` forbidden403
|
||||||
it "get all orders for owner" $ do
|
it "get all orders for owner" $ do
|
||||||
req <-
|
req <-
|
||||||
testGet
|
testGet
|
||||||
"/api/allorders"
|
"/api/allorders"
|
||||||
[ ("address", Just "Zaddy")
|
[("session", Just "35bfb9c2-9ad2-4fe5-adda-99d63b8dcdcd")]
|
||||||
, ("session", Just "35bfb9c2-9ad2-4fe5-adda-99d63b8dcdcd")
|
|
||||||
]
|
|
||||||
res <- httpJSON req
|
res <- httpJSON req
|
||||||
getResponseStatus (res :: Response A.Value) `shouldBe` ok200
|
getResponseStatus (res :: Response A.Value) `shouldBe` ok200
|
||||||
it "get all orders for owner fails with bad session" $ do
|
it "get all orders for owner fails with bad session" $ do
|
||||||
req <-
|
req <-
|
||||||
testGet
|
testGet
|
||||||
"/api/allorders"
|
"/api/allorders"
|
||||||
[ ("address", Just "Zaddy")
|
|
||||||
, ("session", Just "35bfb9c2-9ad2-4fe5-fake-99d63b8dcdcd")
|
|
||||||
]
|
|
||||||
res <- httpLBS req
|
|
||||||
getResponseStatus res `shouldBe` unauthorized401
|
|
||||||
it "delete order by id" $ do
|
|
||||||
req <-
|
|
||||||
testDelete
|
|
||||||
"/api/order/"
|
|
||||||
"627ab3ea2b05a76be3000000"
|
|
||||||
[("session", Just "35bfb9c2-9ad2-4fe5-adda-99d63b8dcdcd")]
|
|
||||||
res <- httpLBS req
|
|
||||||
getResponseStatus res `shouldBe` ok200
|
|
||||||
it "delete order by id fails with bad session" $ do
|
|
||||||
req <-
|
|
||||||
testDelete
|
|
||||||
"/api/order/"
|
|
||||||
"627ab3ea2b05a76be3000000"
|
|
||||||
[("session", Just "35bfb9c2-9ad2-4fe5-fake-99d63b8dcdcd")]
|
[("session", Just "35bfb9c2-9ad2-4fe5-fake-99d63b8dcdcd")]
|
||||||
res <- httpLBS req
|
res <- httpLBS req
|
||||||
getResponseStatus res `shouldBe` unauthorized401
|
getResponseStatus res `shouldBe` unauthorized401
|
||||||
|
@ -475,6 +464,22 @@ main = do
|
||||||
[("session", Just "35bfb9c2-9ad2-4fe5-adda-99d63b8dfake")]
|
[("session", Just "35bfb9c2-9ad2-4fe5-adda-99d63b8dfake")]
|
||||||
res <- httpLBS req
|
res <- httpLBS req
|
||||||
getResponseStatus res `shouldBe` forbidden403
|
getResponseStatus res `shouldBe` forbidden403
|
||||||
|
it "delete order by id fails with bad session" $ do
|
||||||
|
req <-
|
||||||
|
testDelete
|
||||||
|
"/api/order/"
|
||||||
|
"627ab3ea2b05a76be3000000"
|
||||||
|
[("session", Just "35bfb9c2-9ad2-4fe5-fake-99d63b8dcdcd")]
|
||||||
|
res <- httpLBS req
|
||||||
|
getResponseStatus res `shouldBe` unauthorized401
|
||||||
|
it "delete order by id" $ do
|
||||||
|
req <-
|
||||||
|
testDelete
|
||||||
|
"/api/order/"
|
||||||
|
"627ab3ea2b05a76be3000000"
|
||||||
|
[("session", Just "35bfb9c2-9ad2-4fe5-adda-99d63b8dcdcd")]
|
||||||
|
res <- httpLBS req
|
||||||
|
getResponseStatus res `shouldBe` ok200
|
||||||
describe "Item endpoint" $ do
|
describe "Item endpoint" $ do
|
||||||
it "adding item with bad session fails" $ do
|
it "adding item with bad session fails" $ do
|
||||||
let item =
|
let item =
|
||||||
|
@ -748,6 +753,7 @@ main = do
|
||||||
False
|
False
|
||||||
""
|
""
|
||||||
""
|
""
|
||||||
|
"testToken1234"
|
||||||
let ordTest = val myOrder
|
let ordTest = val myOrder
|
||||||
case ordTest of
|
case ordTest of
|
||||||
Doc oT -> access p master "test" (insert_ "orders" oT)
|
Doc oT -> access p master "test" (insert_ "orders" oT)
|
||||||
|
@ -1112,7 +1118,7 @@ startAPI config = do
|
||||||
let myOrder =
|
let myOrder =
|
||||||
ZGoOrder
|
ZGoOrder
|
||||||
(Just (read "627ab3ea2b05a76be3000000"))
|
(Just (read "627ab3ea2b05a76be3000000"))
|
||||||
"Zaddy"
|
"zs1w6nkameazc5gujm69350syl5w8tgvyaphums3pw8eytzy5ym08x7dvskmykkatmwrucmgv3er8e"
|
||||||
"35bfb9c2-9ad2-4fe5-adda-99d63b8dcdcd"
|
"35bfb9c2-9ad2-4fe5-adda-99d63b8dcdcd"
|
||||||
myTs
|
myTs
|
||||||
False
|
False
|
||||||
|
@ -1124,6 +1130,7 @@ startAPI config = do
|
||||||
False
|
False
|
||||||
""
|
""
|
||||||
""
|
""
|
||||||
|
"testToken1234"
|
||||||
let ordTest = val myOrder
|
let ordTest = val myOrder
|
||||||
case ordTest of
|
case ordTest of
|
||||||
Doc oT -> access pipe master "test" (insert_ "orders" oT)
|
Doc oT -> access pipe master "test" (insert_ "orders" oT)
|
||||||
|
@ -1177,7 +1184,8 @@ instance Arbitrary ZGoOrder where
|
||||||
l <- arbitrary
|
l <- arbitrary
|
||||||
pd <- arbitrary
|
pd <- arbitrary
|
||||||
eI <- arbitrary
|
eI <- arbitrary
|
||||||
ZGoOrder i a s ts c cur p t tZ l pd eI <$> arbitrary
|
sc <- arbitrary
|
||||||
|
ZGoOrder i a s ts c cur p t tZ l pd eI sc <$> arbitrary
|
||||||
|
|
||||||
instance Arbitrary LineItem where
|
instance Arbitrary LineItem where
|
||||||
arbitrary = do
|
arbitrary = do
|
||||||
|
|
Loading…
Reference in a new issue