Move yesod-test to using the term "token" instead of nonce.
Addresses part #1 of #914
This commit is contained in:
parent
70b081af8d
commit
94af9c6bdd
@ -50,7 +50,7 @@ This is the helloworld and kitchen sink. In this case for testing a yesod app.
|
|||||||
|
|
||||||
-- Performs the post using the current page to extract field values:
|
-- Performs the post using the current page to extract field values:
|
||||||
post "url/to/post/to" $ do
|
post "url/to/post/to" $ do
|
||||||
addNonce -- Add the _nonce field with the currently shown value
|
addToken -- Add the CSRF _token field with the currently shown value
|
||||||
|
|
||||||
-- Lookup field by the text on the labels pointing to them.
|
-- Lookup field by the text on the labels pointing to them.
|
||||||
byLabel "Email:" "gustavo@cerati.com"
|
byLabel "Email:" "gustavo@cerati.com"
|
||||||
|
|||||||
@ -16,9 +16,9 @@ visited page. You can perform assertions on the content of HTML responses,
|
|||||||
using CSS selectors to explore the document more easily.
|
using CSS selectors to explore the document more easily.
|
||||||
|
|
||||||
You can also easily build requests using forms present in the current page.
|
You can also easily build requests using forms present in the current page.
|
||||||
This is very useful for testing web applications built in yesod, for example,
|
This is very useful for testing web applications built in yesod for example,
|
||||||
where your forms may have field names generated by the framework or a randomly
|
were your forms may have field names generated by the framework or a randomly
|
||||||
generated nonce value.
|
generated '_token' field.
|
||||||
|
|
||||||
Your database is also directly available so you can use 'runDB' to set up
|
Your database is also directly available so you can use 'runDB' to set up
|
||||||
backend pre-conditions, or to assert that your session is having the desired effect.
|
backend pre-conditions, or to assert that your session is having the desired effect.
|
||||||
@ -40,7 +40,7 @@ module Yesod.Test
|
|||||||
-- * Making requests
|
-- * Making requests
|
||||||
-- | You can construct requests with the 'RequestBuilder' monad, which lets you
|
-- | You can construct requests with the 'RequestBuilder' monad, which lets you
|
||||||
-- set the URL and add parameters, headers, and files. Helper functions are provided to
|
-- set the URL and add parameters, headers, and files. Helper functions are provided to
|
||||||
-- lookup fields by label and to add the current nonce value from your forms.
|
-- lookup fields by label and to add the current CSRF token from your forms.
|
||||||
-- Once built, the request can be executed with the 'request' method.
|
-- Once built, the request can be executed with the 'request' method.
|
||||||
--
|
--
|
||||||
-- Convenience functions like 'get' and 'post' build and execute common requests.
|
-- Convenience functions like 'get' and 'post' build and execute common requests.
|
||||||
@ -73,6 +73,8 @@ module Yesod.Test
|
|||||||
--
|
--
|
||||||
-- In order to prevent your forms from being rejected in tests, use one of
|
-- In order to prevent your forms from being rejected in tests, use one of
|
||||||
-- these functions to add the nonce to your request.
|
-- these functions to add the nonce to your request.
|
||||||
|
, addToken
|
||||||
|
, addToken_
|
||||||
, addNonce
|
, addNonce
|
||||||
, addNonce_
|
, addNonce_
|
||||||
|
|
||||||
@ -553,6 +555,16 @@ fileByLabel label path mime = do
|
|||||||
name <- nameFromLabel label
|
name <- nameFromLabel label
|
||||||
addFile name path mime
|
addFile name path mime
|
||||||
|
|
||||||
|
-- | An alias for 'addToken_'.
|
||||||
|
addNonce_ :: Query -> RequestBuilder site ()
|
||||||
|
addNonce_ = addToken_
|
||||||
|
{-# DEPRECATED addNonce_ "Use 'addToken_' instead; 'addNonce_' will be removed in the next major version. Reasoning: Yesod's CSRF tokens are not actually nonces (one-time values), so yesod-form moved to calling them tokens instead. yesod-test is now using the word token as well. See https://github.com/yesodweb/yesod/issues/914 for details." #-}
|
||||||
|
|
||||||
|
-- | An alias for 'addToken'.
|
||||||
|
addNonce :: RequestBuilder site ()
|
||||||
|
addNonce = addToken
|
||||||
|
{-# DEPRECATED addNonce "Use 'addToken' instead; 'addNonce' will be removed in the next major version. Reasoning: Yesod's CSRF tokens are not actually nonces (one-time values), so yesod-form moved to calling them tokens instead. yesod-test is now using the word token as well. See https://github.com/yesodweb/yesod/issues/914 for details." #-}
|
||||||
|
|
||||||
-- | Lookup a _token form field and add its value to the params.
|
-- | Lookup a _token form field and add its value to the params.
|
||||||
-- Receives a CSS selector that should resolve to the form element containing the nonce.
|
-- Receives a CSS selector that should resolve to the form element containing the nonce.
|
||||||
--
|
--
|
||||||
@ -560,17 +572,23 @@ fileByLabel label path mime = do
|
|||||||
--
|
--
|
||||||
-- > request $ do
|
-- > request $ do
|
||||||
-- > addNonce_ "#formID"
|
-- > addNonce_ "#formID"
|
||||||
addNonce_ :: Query -> RequestBuilder site ()
|
addToken_ :: Query -> RequestBuilder site ()
|
||||||
addNonce_ scope = do
|
addToken_ scope = do
|
||||||
matches <- htmlQuery' rbdResponse $ scope <> "input[name=_token][type=hidden][value]"
|
matches <- htmlQuery' rbdResponse $ scope <> "input[name=_token][type=hidden][value]"
|
||||||
case matches of
|
case matches of
|
||||||
[] -> failure $ "No nonce found in the current page"
|
[] -> failure $ "No CSRF token found in the current page"
|
||||||
element:[] -> addPostParam "_token" $ head $ attribute "value" $ parseHTML element
|
element:[] -> addPostParam "_token" $ head $ attribute "value" $ parseHTML element
|
||||||
_ -> failure $ "More than one nonce found in the page"
|
_ -> failure $ "More than one CSRF token found in the page"
|
||||||
|
|
||||||
-- | For responses that display a single form, just lookup the only nonce available.
|
-- | Lookup a _token form field and add its value to the params.
|
||||||
addNonce :: RequestBuilder site ()
|
-- Receives a CSS selector that should resolve to the form element containing the nonce.
|
||||||
addNonce = addNonce_ ""
|
--
|
||||||
|
-- ==== __Examples__
|
||||||
|
--
|
||||||
|
-- > request $ do
|
||||||
|
-- > addNonce_ "#formID"
|
||||||
|
addToken :: RequestBuilder site ()
|
||||||
|
addToken = addToken_ ""
|
||||||
|
|
||||||
-- | Perform a POST request to @url@.
|
-- | Perform a POST request to @url@.
|
||||||
--
|
--
|
||||||
|
|||||||
@ -122,7 +122,7 @@ main = hspec $ do
|
|||||||
setUrl ("/form" :: Text)
|
setUrl ("/form" :: Text)
|
||||||
byLabel "Some Label" "12345"
|
byLabel "Some Label" "12345"
|
||||||
fileByLabel "Some File" "test/main.hs" "text/plain"
|
fileByLabel "Some File" "test/main.hs" "text/plain"
|
||||||
addNonce
|
addToken
|
||||||
statusIs 200
|
statusIs 200
|
||||||
bodyEquals "12345"
|
bodyEquals "12345"
|
||||||
yit "finding html" $ do
|
yit "finding html" $ do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user