Skip to content
Snippets Groups Projects
Commit 0876e0d3 authored by Jean-Paul Calderone's avatar Jean-Paul Calderone
Browse files

Get property tests working

parent 01cd0f1f
No related branches found
No related tags found
1 merge request!2Stripe webhook
...@@ -28,13 +28,15 @@ import Test.Hspec.Wai ...@@ -28,13 +28,15 @@ import Test.Hspec.Wai
, shouldRespondWith , shouldRespondWith
, liftIO , liftIO
) )
import Test.Hspec.Wai.QuickCheck
( property
)
import Test.QuickCheck import Test.QuickCheck
( Property ( Property
, Gen , Gen
, arbitrary , arbitrary
, suchThat , suchThat
, suchThatMap , suchThatMap
, property
, generate , generate
, forAll , forAll
, (===) , (===)
...@@ -47,7 +49,8 @@ import Util.WAI ...@@ -47,7 +49,8 @@ import Util.WAI
( postJSON ( postJSON
) )
import Util.Gen import Util.Gen
( chargeSucceededEvents ( GoodChargeEvent(GoodChargeEvent)
, chargeSucceededEvents
, hasVoucher , hasVoucher
) )
import Util.JSON import Util.JSON
...@@ -83,14 +86,8 @@ stripeAPI = Proxy ...@@ -83,14 +86,8 @@ stripeAPI = Proxy
app :: IO Application app :: IO Application
app = memory >>= return . stripeServer >>= return . serve stripeAPI app = memory >>= return . stripeServer >>= return . serve stripeAPI
aChargeEvent :: IO LazyBS.ByteString
aChargeEvent = encode <$> generate chargeSucceededEvents
spec_webhook :: Spec spec_webhook :: Spec
spec_webhook = with app $ do spec_webhook = with app $ do
-- I would like to make these property tests but I can't figure out how to
-- use QuickCheck (or Hedgehog) to write property tests for web code.
describe "error behavior of POST /webhook" $ do describe "error behavior of POST /webhook" $ do
it "responds to non-JSON Content-Type with 415 (Unsupported Media Type)" $ it "responds to non-JSON Content-Type with 415 (Unsupported Media Type)" $
post "/webhook" "xxx" `shouldRespondWith` 415 post "/webhook" "xxx" `shouldRespondWith` 415
...@@ -98,10 +95,16 @@ spec_webhook = with app $ do ...@@ -98,10 +95,16 @@ spec_webhook = with app $ do
it "responds to JSON non-Event body with 400 (Invalid Request)" $ it "responds to JSON non-Event body with 400 (Invalid Request)" $
postJSON "/webhook" "{}" `shouldRespondWith` 400 postJSON "/webhook" "{}" `shouldRespondWith` 400
-- I would like to make most or all of these into property tests. *This*
-- test shows how you can do it. Yay. The main thing (for me, anyway) to
-- remember is to use `property` from Test.Hspec.Wai.QuickCheck and not from
-- `Test.QuickCheck`. :/ Unsure whether I love the apparent Haskell
-- convention of giving the same name to *similar* functions.
describe "success behavior of POST /webhook" $ describe "success behavior of POST /webhook" $
it "responds to a JSON Event body with 200 (OK)" $ do it "responds to a JSON Event body with 200 (OK)" $
event <- liftIO aChargeEvent property $
postJSON "/webhook" event `shouldRespondWith` 200 { matchBody = MatchBody bodyMatcher } \(GoodChargeEvent event) ->
postJSON "/webhook" (encode event) `shouldRespondWith` 200 { matchBody = MatchBody bodyMatcher }
bodyMatcher :: [Network.HTTP.Types.Header] -> Body -> Maybe String bodyMatcher :: [Network.HTTP.Types.Header] -> Body -> Maybe String
bodyMatcher _ "{}" = Nothing bodyMatcher _ "{}" = Nothing
......
...@@ -4,6 +4,7 @@ module Util.Gen ...@@ -4,6 +4,7 @@ module Util.Gen
( chargeSucceededEvents ( chargeSucceededEvents
, posixTimes , posixTimes
, hasVoucher , hasVoucher
, GoodChargeEvent(GoodChargeEvent)
) where ) where
import Data.Text import Data.Text
...@@ -143,6 +144,13 @@ chargeSucceededEvents = ...@@ -143,6 +144,13 @@ chargeSucceededEvents =
<*> arbitrary -- eventPendingWebHooks <*> arbitrary -- eventPendingWebHooks
<*> arbitrary -- eventRequest <*> arbitrary -- eventRequest
data GoodChargeEvent = GoodChargeEvent Event deriving (Show, Eq)
instance Arbitrary GoodChargeEvent where
arbitrary = chargeSucceededEvents `suchThatMap` (Just . GoodChargeEvent)
posixTimes :: Gen UTCTime posixTimes :: Gen UTCTime
posixTimes = (arbitrary :: Gen Integer) `suchThatMap` (Just . posixSecondsToUTCTime . fromIntegral . abs) posixTimes = (arbitrary :: Gen Integer) `suchThatMap` (Just . posixSecondsToUTCTime . fromIntegral . abs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment