Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PaymentServer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Administrator
PaymentServer
Commits
0876e0d3
Commit
0876e0d3
authored
5 years ago
by
Jean-Paul Calderone
Browse files
Options
Downloads
Patches
Plain Diff
Get property tests working
parent
01cd0f1f
No related branches found
Branches containing commit
No related tags found
1 merge request
!2
Stripe webhook
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
test/SpecStripe.hs
+14
-11
14 additions, 11 deletions
test/SpecStripe.hs
test/Util/Gen.hs
+8
-0
8 additions, 0 deletions
test/Util/Gen.hs
with
22 additions
and
11 deletions
test/SpecStripe.hs
+
14
−
11
View file @
0876e0d3
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
test/Util/Gen.hs
+
8
−
0
View file @
0876e0d3
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment