packages feed

mangopay-1.0: 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
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 Data.Conduit (runResourceT)

-- | 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 $ runMangoPayT newCreds mgr Sandbox createCredentialsSecret
       assertBool (isJust $ cClientSecret  creds2)   
       let s=fromJust $ cClientSecret creds2
       -- login once with our new credentials
       oat<-runResourceT $ 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]