refactored basic auth test cases

This commit is contained in:
Sönke Hahn 2016-04-06 15:24:30 +08:00
parent 14ff219726
commit 6df3429b68
2 changed files with 7 additions and 4 deletions

View file

@ -111,6 +111,7 @@ test-suite spec
base == 4.* base == 4.*
, base-compat , base-compat
, aeson , aeson
, base64-bytestring
, bytestring , bytestring
, bytestring-conversion , bytestring-conversion
, directory , directory

View file

@ -19,8 +19,10 @@ import Control.Applicative ((<$>))
import Control.Monad (forM_, when, unless) import Control.Monad (forM_, when, unless)
import Control.Monad.Trans.Except (ExceptT, throwE) import Control.Monad.Trans.Except (ExceptT, throwE)
import Data.Aeson (FromJSON, ToJSON, decode', encode) import Data.Aeson (FromJSON, ToJSON, decode', encode)
import qualified Data.ByteString.Base64 as Base64
import Data.ByteString.Conversion () import Data.ByteString.Conversion ()
import Data.Char (toUpper) import Data.Char (toUpper)
import Data.Monoid
import Data.Proxy (Proxy (Proxy)) import Data.Proxy (Proxy (Proxy))
import Data.String (fromString) import Data.String (fromString)
import Data.String.Conversions (cs) import Data.String.Conversions (cs)
@ -569,17 +571,17 @@ basicAuthSpec = do
with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do
context "Basic Authentication" $ do context "Basic Authentication" $ do
let basicAuthHeaders user password =
[("Authorization", "Basic " <> Base64.encode (user <> ":" <> password))]
it "returns 401 when no credentials given" $ do it "returns 401 when no credentials given" $ do
get "/basic" `shouldRespondWith` 401 get "/basic" `shouldRespondWith` 401
it "returns 403 when invalid credentials given" $ do it "returns 403 when invalid credentials given" $ do
let invalid = [("Authorization", "Basic bbbbbbbbbDpzZXJ2ZXI=")] -- fixme: how do I create basic auth tokens? THW.request methodGet "/basic" (basicAuthHeaders "servant" "wrong") ""
THW.request methodGet "/basic" invalid ""
`shouldRespondWith` 403 `shouldRespondWith` 403
it "returns 200 with the right password" $ do it "returns 200 with the right password" $ do
let validCredentials = [("Authorization", "Basic c2VydmFudDpzZXJ2ZXI=")] THW.request methodGet "/basic" (basicAuthHeaders "servant" "server") ""
THW.request methodGet "/basic" validCredentials ""
`shouldRespondWith` 200 `shouldRespondWith` 200
it "plays nice with subsequent Raw endpoints" $ do it "plays nice with subsequent Raw endpoints" $ do