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
76e7cd66
Commit
76e7cd66
authored
5 years ago
by
Jean-Paul Calderone
Browse files
Options
Downloads
Patches
Plain Diff
write some more persistence tests (and check them in this time)
parent
8d90b081
No related branches found
Branches containing commit
No related tags found
1 merge request
!2
Stripe webhook
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
PaymentServer.cabal
+1
-0
1 addition, 0 deletions
PaymentServer.cabal
src/PaymentServer/Persistence.hs
+22
-0
22 additions, 0 deletions
src/PaymentServer/Persistence.hs
test/SpecPersistence.hs
+79
-0
79 additions, 0 deletions
test/SpecPersistence.hs
with
102 additions
and
0 deletions
PaymentServer.cabal
+
1
−
0
View file @
76e7cd66
...
...
@@ -41,6 +41,7 @@ test-suite PaymentServer-test
hs-source-dirs: test
main-is: Driver.hs
other-modules: SpecStripe
, SpecPersistence
, Util.WAI
, Util.Gen
, Util.JSON
...
...
This diff is collapsed.
Click to expand it.
src/PaymentServer/Persistence.hs
+
22
−
0
View file @
76e7cd66
module
PaymentServer.Persistence
(
Voucher
,
Fingerprint
,
RedeemError
(
NotPaid
,
AlreadyRedeemed
)
,
VoucherDatabase
(
payForVoucher
,
redeemVoucher
)
,
memory
)
where
import
Control.Monad
(
liftM
)
import
Data.Text
(
Text
)
...
...
@@ -13,6 +18,7 @@ import Data.IORef
(
IORef
,
newIORef
,
modifyIORef
,
readIORef
)
-- | A voucher is a unique identifier which can be associated with a payment.
...
...
@@ -27,6 +33,7 @@ data RedeemError =
NotPaid
-- | The voucher has already been redeemed.
|
AlreadyRedeemed
deriving
(
Show
,
Eq
)
-- | A fingerprint cryptographically identifies a redemption of a voucher.
-- When a voucher is redeemed, a number of random tokens are received
...
...
@@ -70,6 +77,21 @@ instance VoucherDatabase MemoryVoucherDatabase where
modifyIORef
paid
(
Set
.
insert
voucher
)
return
()
redeemVoucher
Memory
{
paid
=
paid
,
redeemed
=
redeemed
}
voucher
fingerprint
=
do
unpaid
<-
(
liftM
$
Set
.
notMember
voucher
)
.
readIORef
$
paid
existingFingerprint
<-
(
liftM
$
Map
.
lookup
voucher
)
.
readIORef
$
redeemed
case
(
unpaid
,
existingFingerprint
)
of
(
True
,
_
)
->
return
$
Left
NotPaid
(
False
,
Nothing
)
->
do
modifyIORef
redeemed
(
Map
.
insert
voucher
fingerprint
)
return
$
Right
()
(
False
,
Just
fingerprint'
)
->
if
fingerprint
==
fingerprint'
then
return
$
Right
()
else
return
$
Left
AlreadyRedeemed
-- | Create a new, empty MemoryVoucherDatabase.
memory
::
IO
MemoryVoucherDatabase
memory
=
do
...
...
This diff is collapsed.
Click to expand it.
test/SpecPersistence.hs
0 → 100644
+
79
−
0
View file @
76e7cd66
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
--
-- Test suite related to the persistence system.
--
module
SpecPersistence
where
import
Test.QuickCheck
(
Property
,
(
===
)
)
import
Control.Monad.IO.Class
(
liftIO
)
import
Test.QuickCheck.Monadic
(
monadicIO
,
run
,
assert
,
pre
)
import
PaymentServer.Persistence
(
RedeemError
(
NotPaid
,
AlreadyRedeemed
)
,
Voucher
,
Fingerprint
,
VoucherDatabase
(
payForVoucher
,
redeemVoucher
)
,
memory
)
-- | A voucher which has not been paid for cannot be redeemed.
unpaidVoucherNotRedeemable
::
VoucherDatabase
d
=>
IO
d
->
Voucher
->
Fingerprint
->
Property
unpaidVoucherNotRedeemable
getDB
voucher
fingerprint
=
monadicIO
$
do
db
<-
liftIO
getDB
result
<-
run
$
redeemVoucher
db
voucher
fingerprint
assert
(
result
==
Left
NotPaid
)
-- | The in-memory implementation for unpaidVoucherNotRedeemable.
prop_memory_unpaidVoucherNotRedeemable
=
unpaidVoucherNotRedeemable
memory
-- | A voucher which is paid for can be redeemed with any fingerprint.
paidVoucherRedeemable
::
VoucherDatabase
d
=>
IO
d
->
Voucher
->
Fingerprint
->
Property
paidVoucherRedeemable
getDB
voucher
fingerprint
=
monadicIO
$
do
db
<-
liftIO
getDB
()
<-
run
$
payForVoucher
db
voucher
result
<-
run
$
redeemVoucher
db
voucher
fingerprint
assert
(
result
==
Right
()
)
-- | The in-memory implementation for paidVoucherRedeemable.
prop_memory_paidVoucherRedeemable
=
paidVoucherRedeemable
memory
-- | A voucher which is paid for can be redeemed more than once as long as the
-- same fingerprint is used each time.
paidVoucherMultiRedeemable
::
VoucherDatabase
d
=>
IO
d
->
Voucher
->
Fingerprint
->
Property
paidVoucherMultiRedeemable
getDB
voucher
fingerprint
=
monadicIO
$
do
db
<-
liftIO
getDB
()
<-
run
$
payForVoucher
db
voucher
let
redeem
=
redeemVoucher
db
voucher
fingerprint
run
redeem
result
<-
run
redeem
assert
(
result
==
Right
()
)
-- | The in-memory implementation for paidVoucherMultiRedeemable.
prop_memory_paidVoucherMultiRedeemable
=
paidVoucherMultiRedeemable
memory
-- | A voucher which is paid for can not be redeemed a second time with a
-- different fingerprint than was used on the first attempt.
paidVoucherMismatchFingerprint
::
VoucherDatabase
d
=>
IO
d
->
Voucher
->
Fingerprint
->
Fingerprint
->
Property
paidVoucherMismatchFingerprint
getDB
voucher
fingerprint
fingerprint'
=
monadicIO
$
do
pre
(
fingerprint
/=
fingerprint'
)
db
<-
liftIO
getDB
()
<-
run
$
payForVoucher
db
voucher
let
redeem
=
redeemVoucher
db
voucher
run
$
redeem
fingerprint
result
<-
run
$
redeem
fingerprint'
assert
(
result
==
Left
AlreadyRedeemed
)
-- | The in-memory implementation for paidVoucherMismatchFingerprint.
prop_memory_paidVoucherMismatchFingerprint
=
paidVoucherMismatchFingerprint
memory
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