|
1 | 1 | module ApiSpec (apiSpec) where
|
2 | 2 |
|
3 |
| -import Network.HTTP.Client |
4 |
| -import Network.HTTP.Client.TLS |
5 |
| -import Test.Hspec |
| 3 | +import qualified Data.Text as T |
6 | 4 | import Data.Time
|
7 | 5 | import Data.Time.TimeSpan
|
8 |
| -import System.Environment (getEnv) |
9 |
| -import qualified Data.Text as T |
10 | 6 | import qualified Data.Vector as V
|
11 |
| - |
| 7 | +import Network.HTTP.Client |
| 8 | +import Network.HTTP.Client.TLS |
12 | 9 | import Stripe.Client
|
| 10 | +import System.Environment (getEnv) |
| 11 | +import Test.Hspec |
13 | 12 |
|
14 | 13 | makeClient :: IO StripeClient
|
15 | 14 | makeClient =
|
16 |
| - do manager <- newManager tlsManagerSettings |
17 |
| - apiKey <- T.pack <$> getEnv "STRIPE_KEY" |
18 |
| - pure (makeStripeClient apiKey manager 2) |
| 15 | + do |
| 16 | + manager <- newManager tlsManagerSettings |
| 17 | + apiKey <- T.pack <$> getEnv "STRIPE_KEY" |
| 18 | + pure (makeStripeClient apiKey manager 2) |
19 | 19 |
|
20 | 20 | forceSuccess :: (MonadFail m, Show a) => m (Either a b) -> m b
|
21 | 21 | forceSuccess req =
|
22 | 22 | req >>= \res ->
|
23 |
| - case res of |
24 |
| - Left err -> fail (show err) |
25 |
| - Right ok -> pure ok |
| 23 | + case res of |
| 24 | + Left err -> fail (show err) |
| 25 | + Right ok -> pure ok |
26 | 26 |
|
27 | 27 | apiSpec :: Spec
|
28 | 28 | apiSpec =
|
29 |
| - do describe "core api" apiTests |
30 |
| - describe "api" apiWorldTests |
| 29 | + do |
| 30 | + describe "core api" apiTests |
| 31 | + describe "api" apiWorldTests |
31 | 32 |
|
32 | 33 | apiTests :: SpecWith ()
|
33 | 34 | apiTests =
|
34 | 35 | beforeAll makeClient $
|
35 |
| - do describe "events" $ |
36 |
| - do it "lists events" $ \cli -> |
37 |
| - do _ <- forceSuccess $ createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
38 |
| - res <- forceSuccess $ listEvents cli Nothing |
39 |
| - V.null (slData res) `shouldBe` False |
40 |
| - describe "products" $ |
41 |
| - do it "creates a product" $ \cli -> |
42 |
| - do res <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
43 |
| - prName res `shouldBe` "Test" |
| 36 | + do |
| 37 | + describe "events" $ |
| 38 | + do |
| 39 | + it "lists events" $ \cli -> |
| 40 | + do |
| 41 | + _ <- |
| 42 | + forceSuccess $ |
| 43 | + createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
| 44 | + res <- forceSuccess $ listEvents cli Nothing |
| 45 | + V.null (slData res) `shouldBe` False |
| 46 | + describe "products" $ |
| 47 | + do |
| 48 | + it "creates a product" $ \cli -> |
| 49 | + do |
| 50 | + res <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
| 51 | + prName res `shouldBe` "Test" |
44 | 52 | it "retrieves a product" $ \cli ->
|
45 |
| - do res <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
46 |
| - res2 <- forceSuccess $ retrieveProduct cli (prId res) |
47 |
| - res `shouldBe` res2 |
48 |
| - describe "prices" $ |
49 |
| - do it "creates a price" $ \cli -> |
50 |
| - do prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
51 |
| - res <- |
52 |
| - forceSuccess $ |
53 |
| - createPrice cli $ |
54 |
| - PriceCreate "usd" (Just 1000) (prId prod) (Just "lk") True $ |
55 |
| - Just (PriceCreateRecurring "month" Nothing) |
56 |
| - pCurrency res `shouldBe` "usd" |
57 |
| - pUnitAmount res `shouldBe` Just 1000 |
58 |
| - pType res `shouldBe` "recurring" |
59 |
| - pLookupKey res `shouldBe` Just "lk" |
60 |
| - pRecurring res `shouldBe` Just (PriceRecurring "month" 1) |
| 53 | + do |
| 54 | + res <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
| 55 | + res2 <- forceSuccess $ retrieveProduct cli (prId res) |
| 56 | + res `shouldBe` res2 |
| 57 | + describe "prices" $ |
| 58 | + do |
| 59 | + it "creates a price" $ \cli -> |
| 60 | + do |
| 61 | + prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
| 62 | + res <- |
| 63 | + forceSuccess $ |
| 64 | + createPrice cli $ |
| 65 | + PriceCreate "usd" (Just 1000) (prId prod) (Just "lk") True $ |
| 66 | + Just (PriceCreateRecurring "month" Nothing) |
| 67 | + pCurrency res `shouldBe` "usd" |
| 68 | + pUnitAmount res `shouldBe` Just 1000 |
| 69 | + pType res `shouldBe` "recurring" |
| 70 | + pLookupKey res `shouldBe` Just "lk" |
| 71 | + pRecurring res `shouldBe` Just (PriceRecurring "month" 1) |
61 | 72 | it "retrieves a price" $ \cli ->
|
62 |
| - do prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
63 |
| - res <- |
64 |
| - forceSuccess $ |
65 |
| - createPrice cli $ |
66 |
| - PriceCreate "usd" (Just 1000) (prId prod) Nothing False $ |
67 |
| - Just (PriceCreateRecurring "month" Nothing) |
68 |
| - res2 <- forceSuccess $ retrievePrice cli (pId res) |
69 |
| - res `shouldBe` res2 |
| 73 | + do |
| 74 | + prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
| 75 | + res <- |
| 76 | + forceSuccess $ |
| 77 | + createPrice cli $ |
| 78 | + PriceCreate "usd" (Just 1000) (prId prod) Nothing False $ |
| 79 | + Just (PriceCreateRecurring "month" Nothing) |
| 80 | + res2 <- forceSuccess $ retrievePrice cli (pId res) |
| 81 | + res `shouldBe` res2 |
70 | 82 | it "lists by lookup_key" $ \cli ->
|
71 |
| - do prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
72 |
| - price <- |
73 |
| - forceSuccess $ |
74 |
| - createPrice cli $ |
75 |
| - PriceCreate "usd" (Just 1000) (prId prod) (Just "the_key") True $ |
76 |
| - Just (PriceCreateRecurring "month" Nothing) |
77 |
| - res <- forceSuccess $ listPrices cli (Just "the_key") |
78 |
| - pId (V.head (slData res)) `shouldBe` pId price |
79 |
| - res2 <- forceSuccess $ listPrices cli (Just "KEY_NOT_EXISTING_OK") |
80 |
| - V.null (slData res2) `shouldBe` True |
81 |
| - describe "customers" $ |
82 |
| - do it "creates a customer" $ \cli -> |
83 |
| - do cr <- forceSuccess $ createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
84 |
| - cEmail cr `shouldBe` Just "[email protected]" |
| 83 | + do |
| 84 | + prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
| 85 | + price <- |
| 86 | + forceSuccess $ |
| 87 | + createPrice cli $ |
| 88 | + PriceCreate "usd" (Just 1000) (prId prod) (Just "the_key") True $ |
| 89 | + Just (PriceCreateRecurring "month" Nothing) |
| 90 | + res <- forceSuccess $ listPrices cli (Just "the_key") |
| 91 | + pId (V.head (slData res)) `shouldBe` pId price |
| 92 | + res2 <- forceSuccess $ listPrices cli (Just "KEY_NOT_EXISTING_OK") |
| 93 | + V.null (slData res2) `shouldBe` True |
| 94 | + describe "customers" $ |
| 95 | + do |
| 96 | + it "creates a customer" $ \cli -> |
| 97 | + do |
| 98 | + cr <- |
| 99 | + forceSuccess $ |
| 100 | + createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
| 101 | + cEmail cr `shouldBe` Just "[email protected]" |
85 | 102 | it "retrieves a customer" $ \cli ->
|
86 |
| - do cr <- forceSuccess $ createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
87 |
| - cu <- forceSuccess $ retrieveCustomer cli (cId cr) |
88 |
| - cu `shouldBe` cr |
| 103 | + do |
| 104 | + cr <- |
| 105 | + forceSuccess $ |
| 106 | + createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
| 107 | + cu <- forceSuccess $ retrieveCustomer cli (cId cr) |
| 108 | + cu `shouldBe` cr |
89 | 109 | it "updates a customer" $ \cli ->
|
90 |
| - do cr <- forceSuccess $ createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
91 |
| - cu <- forceSuccess $ updateCustomer cli (cId cr) ( CustomerUpdate Nothing ( Just "[email protected]")) |
92 |
| - cEmail cu `shouldBe` Just "[email protected]" |
| 110 | + do |
| 111 | + cr <- |
| 112 | + forceSuccess $ |
| 113 | + createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
| 114 | + cu <- |
| 115 | + forceSuccess $ |
| 116 | + updateCustomer |
| 117 | + cli |
| 118 | + (cId cr) |
| 119 | + ( CustomerUpdate Nothing ( Just "[email protected]")) |
| 120 | + cEmail cu `shouldBe` Just "[email protected]" |
93 | 121 |
|
94 |
| -data StripeWorld |
95 |
| - = StripeWorld |
96 |
| - { swProduct :: Product |
97 |
| - , swPrice :: Price |
98 |
| - , swCustomer :: Customer |
99 |
| - } deriving (Show, Eq) |
| 122 | +data StripeWorld = StripeWorld |
| 123 | + { swProduct :: Product, |
| 124 | + swPrice :: Price, |
| 125 | + swCustomer :: Customer |
| 126 | + } |
| 127 | + deriving (Show, Eq) |
100 | 128 |
|
101 | 129 | makeStripeWorld :: IO (StripeClient, StripeWorld)
|
102 | 130 | makeStripeWorld =
|
103 |
| - do cli <- makeClient |
104 |
| - customer <- |
105 |
| - forceSuccess $ |
106 |
| - createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
107 |
| - prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
108 |
| - price <- |
109 |
| - forceSuccess $ |
110 |
| - createPrice cli $ |
111 |
| - PriceCreate "usd" (Just 1000) (prId prod) Nothing False $ |
112 |
| - Just (PriceCreateRecurring "month" Nothing) |
113 |
| - pure (cli, StripeWorld prod price customer) |
| 131 | + do |
| 132 | + cli <- makeClient |
| 133 | + customer <- |
| 134 | + forceSuccess $ |
| 135 | + createCustomer cli ( CustomerCreate Nothing ( Just "[email protected]")) |
| 136 | + prod <- forceSuccess $ createProduct cli (ProductCreate "Test" Nothing) |
| 137 | + price <- |
| 138 | + forceSuccess $ |
| 139 | + createPrice cli $ |
| 140 | + PriceCreate "usd" (Just 1000) (prId prod) Nothing False $ |
| 141 | + Just (PriceCreateRecurring "month" Nothing) |
| 142 | + pure (cli, StripeWorld prod price customer) |
114 | 143 |
|
115 | 144 | apiWorldTests :: SpecWith ()
|
116 | 145 | apiWorldTests =
|
117 | 146 | beforeAll makeStripeWorld $
|
118 |
| - do describe "subscriptions" $ |
119 |
| - do it "allows creating a subscription" $ \(cli, sw) -> |
120 |
| - do trialEnd <- TimeStamp . addUTCTimeTS (hours 1) <$> getCurrentTime |
121 |
| - subscription <- |
122 |
| - forceSuccess $ |
123 |
| - createSubscription cli $ |
124 |
| - SubscriptionCreate |
125 |
| - { scCustomer = cId (swCustomer sw) |
126 |
| - , scItems = [SubscriptionCreateItem (pId (swPrice sw)) (Just 1)] |
127 |
| - , scCancelAtPeriodEnd = Just False |
128 |
| - , scTrialEnd = Just trialEnd |
129 |
| - } |
130 |
| - sCancelAtPeriodEnd subscription `shouldBe` False |
131 |
| - sCustomer subscription `shouldBe` cId (swCustomer sw) |
132 |
| - let items = sItems subscription |
133 |
| - fmap siPrice items `shouldBe` pure (swPrice sw) |
134 |
| - fmap siQuantity items `shouldBe` pure (Just 1) |
135 |
| - fmap siSubscription items `shouldBe` pure (sId subscription) |
136 |
| - sStatus subscription `shouldBe` "trialing" |
137 |
| - describe "customer portal" $ |
138 |
| - do it "allows creating a customer portal (needs setup in dashboard)" $ \(cli, sw) -> |
139 |
| - do portal <- |
140 |
| - forceSuccess $ |
141 |
| - createCustomerPortal cli (CustomerPortalCreate (cId (swCustomer sw)) (Just "https://athiemann.net/return")) |
142 |
| - cpCustomer portal `shouldBe` cId (swCustomer sw) |
143 |
| - cpReturnUrl portal `shouldBe` Just "https://athiemann.net/return" |
144 |
| - describe "checkout" $ |
145 |
| - do it "create and retrieves a checkout session" $ \(cli, sw) -> |
146 |
| - do session <- |
147 |
| - forceSuccess $ |
148 |
| - createCheckoutSession cli $ |
149 |
| - CheckoutSessionCreate |
150 |
| - { cscCancelUrl = "https://athiemann.net/cancel" |
151 |
| - , cscMode = "subscription" |
152 |
| - , cscPaymentMethodTypes = ["card"] |
153 |
| - , cscSuccessUrl = "https://athiemann.net/success" |
154 |
| - , cscClientReferenceId = Just "cool" |
155 |
| - , cscCustomer = Just (cId (swCustomer sw)) |
156 |
| - , cscAllowPromotionCodes = Just True |
157 |
| - , cscLineItems = [CheckoutSessionCreateLineItem (pId (swPrice sw)) 1] |
158 |
| - } |
159 |
| - csClientReferenceId session `shouldBe` Just "cool" |
160 |
| - csCancelUrl session `shouldBe` "https://athiemann.net/cancel" |
161 |
| - csSuccessUrl session `shouldBe` "https://athiemann.net/success" |
162 |
| - csPaymentMethodTypes session `shouldBe` V.singleton "card" |
163 |
| - csAllowPromotionCodes session `shouldBe` Just True |
| 147 | + do |
| 148 | + describe "subscriptions" $ |
| 149 | + do |
| 150 | + it "allows creating a subscription" $ \(cli, sw) -> |
| 151 | + do |
| 152 | + trialEnd <- TimeStamp . addUTCTimeTS (hours 1) <$> getCurrentTime |
| 153 | + subscription <- |
| 154 | + forceSuccess $ |
| 155 | + createSubscription cli $ |
| 156 | + SubscriptionCreate |
| 157 | + { scCustomer = cId (swCustomer sw), |
| 158 | + scItems = [SubscriptionCreateItem (pId (swPrice sw)) (Just 1)], |
| 159 | + scCancelAtPeriodEnd = Just False, |
| 160 | + scTrialEnd = Just trialEnd |
| 161 | + } |
| 162 | + sCancelAtPeriodEnd subscription `shouldBe` False |
| 163 | + sCustomer subscription `shouldBe` cId (swCustomer sw) |
| 164 | + let items = sItems subscription |
| 165 | + fmap siPrice items `shouldBe` pure (swPrice sw) |
| 166 | + fmap siQuantity items `shouldBe` pure (Just 1) |
| 167 | + fmap siSubscription items `shouldBe` pure (sId subscription) |
| 168 | + sStatus subscription `shouldBe` SsTrialing |
| 169 | + describe "customer portal" $ |
| 170 | + do |
| 171 | + it "allows creating a customer portal (needs setup in dashboard)" $ \(cli, sw) -> |
| 172 | + do |
| 173 | + portal <- |
| 174 | + forceSuccess $ |
| 175 | + createCustomerPortal |
| 176 | + cli |
| 177 | + ( CustomerPortalCreate (cId (swCustomer sw)) (Just "https://athiemann.net/return") |
| 178 | + ) |
| 179 | + cpCustomer portal `shouldBe` cId (swCustomer sw) |
| 180 | + cpReturnUrl portal `shouldBe` Just "https://athiemann.net/return" |
| 181 | + describe "checkout" $ |
| 182 | + do |
| 183 | + it "create and retrieves a checkout session" $ \(cli, sw) -> |
| 184 | + do |
| 185 | + session <- |
| 186 | + forceSuccess $ |
| 187 | + createCheckoutSession cli $ |
| 188 | + CheckoutSessionCreate |
| 189 | + { cscCancelUrl = "https://athiemann.net/cancel", |
| 190 | + cscMode = "subscription", |
| 191 | + cscPaymentMethodTypes = ["card"], |
| 192 | + cscSuccessUrl = "https://athiemann.net/success", |
| 193 | + cscClientReferenceId = Just "cool", |
| 194 | + cscCustomer = Just (cId (swCustomer sw)), |
| 195 | + cscAllowPromotionCodes = Just True, |
| 196 | + cscLineItems = [CheckoutSessionCreateLineItem (pId (swPrice sw)) 1] |
| 197 | + } |
| 198 | + csClientReferenceId session `shouldBe` Just "cool" |
| 199 | + csCancelUrl session `shouldBe` "https://athiemann.net/cancel" |
| 200 | + csSuccessUrl session `shouldBe` "https://athiemann.net/success" |
| 201 | + csPaymentMethodTypes session `shouldBe` V.singleton "card" |
| 202 | + csAllowPromotionCodes session `shouldBe` Just True |
164 | 203 |
|
165 |
| - sessionRetrieved <- |
166 |
| - forceSuccess $ |
167 |
| - retrieveCheckoutSession cli (csId session) |
168 |
| - sessionRetrieved `shouldBe` session |
| 204 | + sessionRetrieved <- |
| 205 | + forceSuccess $ |
| 206 | + retrieveCheckoutSession cli (csId session) |
| 207 | + sessionRetrieved `shouldBe` session |
0 commit comments