packages feed

mangopay-1.9: test/Web/MangoPay/AccessTest.hs

{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -F -pgmF htfpp #-}
-- | test access functions that can be tested
module Web.MangoPay.AccessTest where


import Web.MangoPay hiding (createHook)
import Web.MangoPay.TestUtils

import Test.Framework
import Test.HUnit (Assertion)
import Data.Aeson
import qualified Data.Text as T
import Data.Maybe (fromJust, isJust)
import Data.ByteString.Lazy as BS
import Data.Time.Clock.POSIX (getPOSIXTime)
import Data.IORef (modifyIORef,readIORef)
import Control.Monad (liftM)
import Control.Monad.Trans.Resource (runResourceT)
import Control.Monad.Logger

-- | Take the name/email from client.conf in the current directory
-- (this file can be generated by mangopay-passphrase)
-- generates a new id/name, keeps the same email and generates a new secret
-- saves the secret to use in other tests
test_CreateCredentials :: Assertion
test_CreateCredentials=do
       js<-BS.readFile "client.conf"
       let mcred=decode js
       assertBool (isJust mcred)
       ct<-getPOSIXTime
       -- max 20 characters in ClientId
       let suff=T.take 20 $ T.pack $ show $ round ct
       let creds=(fromJust mcred)
       let newCreds=creds{cClientSecret=Nothing,
                cClientId=T.append (cClientId creds) suff,
                cName=T.append (cName creds) suff}
       mgr<-liftM tsManager $ readIORef testState
       creds2<-runResourceT $ runStdoutLoggingT $ runMangoPayT newCreds mgr Sandbox createCredentialsSecret
       assertBool (isJust $ cClientSecret  creds2)
       let s=fromJust $ cClientSecret creds2
       -- login once with our new credentials
       oat<-runResourceT $ runStdoutLoggingT $ runMangoPayT creds2 mgr Sandbox $
                oauthLogin (cClientId creds2) s
       -- store access token and credentials
       modifyIORef testState (\ts->ts{tsAccessToken=toAccessToken oat,tsCredentials=creds2})
       -- create hooks for all event types
       mapM_ createHook [minBound .. maxBound]