| 1 |
{-| |
| 2 |
Test suite for hledger-web. |
| 3 |
|
| 4 |
Dev notes: |
| 5 |
|
| 6 |
http://hspec.github.io/writing-specs.html |
| 7 |
|
| 8 |
https://hackage.haskell.org/package/yesod-test-1.6.10/docs/Yesod-Test.html |
| 9 |
|
| 10 |
"The best way to see an example project using yesod-test is to create a scaffolded Yesod project: |
| 11 |
stack new projectname yesodweb/sqlite |
| 12 |
(See https://github.com/commercialhaskell/stack-templates/wiki#yesod for the full list of Yesod templates)" |
| 13 |
|
| 14 |
|
| 15 |
These tests don't exactly match the production code path, eg these bits are missing: |
| 16 |
|
| 17 |
withJournal copts (web wopts) -- extra withJournal logic (journalTransform..) |
| 18 |
... |
| 19 |
-- query logic, more options logic |
| 20 |
let depthlessinitialq = filterQuery (not . queryIsDepth) . _rsQuery . reportspec_ $ cliopts_ wopts |
| 21 |
j' = filterJournalTransactions depthlessinitialq j |
| 22 |
h = host_ wopts |
| 23 |
p = port_ wopts |
| 24 |
u = base_url_ wopts |
| 25 |
staticRoot = T.pack <$> file_url_ wopts |
| 26 |
appconfig = AppConfig{appEnv = Development |
| 27 |
,appHost = fromString h |
| 28 |
,appPort = p |
| 29 |
,appRoot = T.pack u |
| 30 |
,appExtra = Extra "" Nothing staticRoot |
| 31 |
} |
| 32 |
|
| 33 |
The production code path, when called in this test context, which I guess is using |
| 34 |
yesod's dev mode, needs to read ./config/settings.yml and fails without it (loadConfig). |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
{-# LANGUAGE OverloadedStrings #-} |
| 39 |
|
| 40 |
module Hledger.Web.Test ( |
| 41 |
hledgerWebTest |
| 42 |
) where |
| 43 |
|
| 44 |
import Data.String (fromString) |
| 45 |
import Data.Function ((&)) |
| 46 |
import Data.Text qualified as T |
| 47 |
import Test.Hspec (hspec) |
| 48 |
import Yesod.Default.Config |
| 49 |
import Yesod.Test |
| 50 |
|
| 51 |
import Hledger.Web.Application ( makeAppWith ) |
| 52 |
import Hledger.Web.WebOptions -- ( WebOpts(..), defwebopts, prognameandversion ) |
| 53 |
import Hledger.Web.Import hiding (get, j) |
| 54 |
import Hledger.Cli hiding (prognameandversion) |
| 55 |
|
| 56 |
|
| 57 |
-- | Given a tests description, zero or more raw option name/value pairs, |
| 58 |
-- a journal and some hspec tests, parse the options and configure the |
| 59 |
-- web app more or less as we normally would (see details above), then run the tests. |
| 60 |
-- |
| 61 |
-- Raw option names are like the long flag without the --, eg "file" or "base-url". |
| 62 |
-- |
| 63 |
-- The journal and raw options should correspond enough to not cause problems. |
| 64 |
-- Be cautious - without a [("file", "somepath")], perhaps journalReload could load |
| 65 |
-- the user's default journal. |
| 66 |
-- |
| 67 |
runTests :: String -> [(String,String)] -> Journal -> YesodSpec App -> IO () |
| 68 |
runTests testsdesc rawopts j tests = do |
| 69 |
wopts <- rawOptsToWebOpts $ mkRawOpts rawopts |
| 70 |
let yconf = AppConfig{ -- :: AppConfig DefaultEnv Extra |
| 71 |
appEnv = Testing |
| 72 |
-- https://hackage.haskell.org/package/conduit-extra/docs/Data-Conduit-Network.html#t:HostPreference |
| 73 |
-- ,appHost = "*4" -- "any IPv4 or IPv6 hostname, IPv4 preferred" |
| 74 |
-- ,appPort = 3000 -- force a port for tests ? |
| 75 |
-- Test with the host and port from opts. XXX more fragile, can clash with a running instance ? |
| 76 |
,appHost = host_ wopts & fromString |
| 77 |
,appPort = port_ wopts |
| 78 |
,appRoot = base_url_ wopts & T.pack -- XXX not sure this or extraStaticRoot get used |
| 79 |
,appExtra = Extra |
| 80 |
{ extraCopyright = "" |
| 81 |
, extraAnalytics = Nothing |
| 82 |
, extraStaticRoot = T.pack <$> file_url_ wopts |
| 83 |
} |
| 84 |
} |
| 85 |
app <- makeAppWith j yconf wopts |
| 86 |
hspec $ yesodSpec app $ ydescribe testsdesc tests -- https://hackage.haskell.org/package/yesod-test/docs/Yesod-Test.html |
| 87 |
|
| 88 |
-- | Run hledger-web's built-in tests using the hspec test runner. |
| 89 |
hledgerWebTest :: IO () |
| 90 |
hledgerWebTest = do |
| 91 |
putStrLn $ "Running tests for " ++ prognameandversion -- ++ " (--test --help for options)" |
| 92 |
let d = fromGregorian 2000 1 1 |
| 93 |
|
| 94 |
runTests "hledger-web" [] nulljournal $ do |
| 95 |
|
| 96 |
yit "serves a reasonable-looking journal page" $ do |
| 97 |
get JournalR |
| 98 |
statusIs 200 |
| 99 |
bodyContains "Add a transaction" |
| 100 |
|
| 101 |
yit "serves a reasonable-looking register page" $ do |
| 102 |
get RegisterR |
| 103 |
statusIs 200 |
| 104 |
bodyContains "accounts" |
| 105 |
|
| 106 |
yit "hyperlinks use a base url made from the default host and port" $ do |
| 107 |
get JournalR |
| 108 |
statusIs 200 |
| 109 |
let defaultbaseurl = defbaseurl defhost defport |
| 110 |
bodyContains ("href=\"" ++ defaultbaseurl) |
| 111 |
bodyContains ("src=\"" ++ defaultbaseurl) |
| 112 |
|
| 113 |
-- WIP |
| 114 |
-- yit "shows the add form" $ do |
| 115 |
-- get JournalR |
| 116 |
-- -- printBody |
| 117 |
-- -- let addbutton = "button:contains('add')" |
| 118 |
-- -- bodyContains addbutton |
| 119 |
-- -- htmlAnyContain "button:visible" "add" |
| 120 |
-- printMatches "div#addmodal:visible" |
| 121 |
-- htmlCount "div#addmodal:visible" 0 |
| 122 |
|
| 123 |
-- -- clickOn "a#addformlink" |
| 124 |
-- -- printBody |
| 125 |
-- -- bodyContains addbutton |
| 126 |
|
| 127 |
-- yit "can add transactions" $ do |
| 128 |
|
| 129 |
usecolor <- useColorOnStdout |
| 130 |
let |
| 131 |
rawopts = [("forecast","")] |
| 132 |
iopts = rawOptsToInputOpts d usecolor True $ mkRawOpts rawopts |
| 133 |
f = "fake" -- need a non-null filename so forecast transactions get index 0 |
| 134 |
pj <- readJournal'' (T.pack $ unlines -- PARTIAL: readJournal'' should not fail |
| 135 |
["~ monthly" |
| 136 |
," assets 10" |
| 137 |
," income" |
| 138 |
]) |
| 139 |
j <- fmap (either error' id) . runExceptT $ journalFinalise iopts f "" pj -- PARTIAL: journalFinalise should not fail |
| 140 |
runTests "hledger-web with --forecast" rawopts j $ do |
| 141 |
|
| 142 |
yit "shows forecasted transactions" $ do |
| 143 |
get JournalR |
| 144 |
statusIs 200 |
| 145 |
bodyContains "id=\"transaction-2-1\"" |
| 146 |
bodyContains "id=\"transaction-2-2\"" |
| 147 |
|
| 148 |
-- #2127 |
| 149 |
-- XXX I'm pretty sure this test lies, ie does not match production behaviour. |
| 150 |
-- (test with curl -s http://localhost:5000/journal | rg '(href)="[\w/].*?"' -o ) |
| 151 |
-- App root setup is a maze of twisty passages, all alike. |
| 152 |
-- runTests "hledger-web with --base-url" |
| 153 |
-- [("base-url","https://base")] nulljournal $ do |
| 154 |
-- yit "hyperlinks respect --base-url" $ do |
| 155 |
-- get JournalR |
| 156 |
-- statusIs 200 |
| 157 |
-- bodyContains "href=\"https://base" |
| 158 |
-- bodyContains "src=\"https://base" |
| 159 |
|
| 160 |
-- #2139 |
| 161 |
-- XXX Not passing. |
| 162 |
-- Static root setup is a maze of twisty passages, all different. |
| 163 |
-- runTests "hledger-web with --base-url, --file-url" |
| 164 |
-- [("base-url","https://base"), ("file-url","https://files")] nulljournal $ do |
| 165 |
-- yit "static file hyperlinks respect --file-url, others respect --base-url" $ do |
| 166 |
-- get JournalR |
| 167 |
-- statusIs 200 |
| 168 |
-- bodyContains "href=\"https://base" |
| 169 |
-- bodyContains "src=\"https://files" |
| 170 |
|