Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Test suite related to voucher redemption.
module SpecRedemption where
import Text.Printf
( printf
)
import Control.Exception
( bracket
)
import Servant
( Application
, Proxy(Proxy)
, serve
)
import Test.Hspec
( Spec
, parallel
, describe
, it
, before
, around
, shouldReturn
, shouldBe
)
import Test.Hspec.Wai
( with
, post
, shouldRespondWith
, liftIO
)
import Test.Hspec.Wai.QuickCheck
( property
)
import Util.Spec
( wrongMethodNotAllowed
, nonJSONUnsupportedMediaType
, wrongJSONInvalidRequest
)
import PaymentServer.Redemption
( RedemptionAPI
, redemptionServer
)
import PaymentServer.Persistence
( Voucher
, Fingerprint
, VoucherDatabase(payForVoucher, redeemVoucher)
, MemoryVoucherDatabase
, memory
)
redemptionAPI :: Proxy RedemptionAPI
redemptionAPI = Proxy
app :: VoucherDatabase d => d -> Application
app = serve redemptionAPI . redemptionServer
path = "/"
spec_simple :: Spec
spec_simple = with (app <$> memory) $ parallel $ do
describe (printf "error behavior of POST %s" (show path)) $ do
wrongMethodNotAllowed "GET" path
nonJSONUnsupportedMediaType path
wrongJSONInvalidRequest path "{}"
withConnection :: VoucherDatabase d => IO d -> ((d -> IO ()) -> IO ())
withConnection getDB = bracket getDB (\db -> return ())
spec_db :: Spec
spec_db = do
around (withConnection memory) $ do
describe "redemptionServer" $ do
it "responds to redemption of an unpaid voucher with 400 (Invalid Request)" $
\(db :: MemoryVoucherDatabase) -> do
payForVoucher db "abcdefg"