summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/test
diff options
context:
space:
mode:
Diffstat (limited to 'lambda-calcul/haskell/test')
-rw-r--r--lambda-calcul/haskell/test/Minilang/ServerSpec.hs36
1 files changed, 25 insertions, 11 deletions
diff --git a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs
index d2a95d1..50c1590 100644
--- a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs
+++ b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs
@@ -5,14 +5,17 @@ module Minilang.ServerSpec where
import Control.Monad (void)
import Data.Aeson (FromJSON, eitherDecode, encode)
+import Data.Bifunctor (first)
import Data.ByteString (isInfixOf, toStrict)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64.Lazy as Base64
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as LBS
import Data.Data (Proxy (..))
+import Data.String (fromString)
+import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Encoding (decodeUtf8)
-import Minilang.API (Challenge (..), Score, User (..), newToken)
+import Minilang.API (Challenge (..), Score, User (..), defaultChallenges, newToken)
import Minilang.App (makeEnv)
import Minilang.Server (serveAPI)
import Network.Wai (Application)
@@ -21,7 +24,6 @@ import System.Random (StdGen, mkStdGen64)
import Test.Hspec (Spec, it)
import Test.Hspec.Wai (WaiSession, get, matchBody, request, shouldRespondWith, with)
import Test.Hspec.Wai.Matcher (MatchBody (..))
-import Data.Bifunctor (first)
spec :: Spec
spec = with (app testSeed) $ do
@@ -58,18 +60,12 @@ spec = with (app testSeed) $ do
`shouldRespondWith` 400
it "GET /api/challenge returns next challenge given user is registered" $ do
- let expected = encode $ Challenge {level = 0, expr = "x"}
+ let expected = LT.unpack $ decodeUtf8 $ encode $ Challenge {level = 0, expr = "x"}
postJSON "/api/register" alice
`shouldRespondWith` 201
get ("/api/challenge?token=" <> LBS.toStrict token)
- `shouldRespondWith` 200
- { matchBody = MatchBody $ \_ b ->
- if b == expected
- then
- Nothing
- else Just ("Expected challenge " ++ show expected ++ ", but got " ++ show b)
- }
+ `shouldRespondWith` fromString expected
it "GET /api/challenge returns 400 given user is not registered" $
get ("/api/challenge?token=" <> LBS.toStrict token)
@@ -102,6 +98,24 @@ spec = with (app testSeed) $ do
postJSON ("/api/challenge?token=" <> LBS.toStrict otherToken) response
`shouldRespondWith` 400
+ it "GET /api/challenge returns next level challenge after successful POST" $ do
+ let expected = LT.unpack $ decodeUtf8 $ encode $ Challenge {level = 1, expr = "y"}
+
+ void $ postJSON "/api/register" alice
+ void $ postJSON ("/api/challenge?token=" <> LBS.toStrict token) "\"x\""
+
+ get ("/api/challenge?token=" <> LBS.toStrict token)
+ `shouldRespondWith` fromString expected
+
+ it "GET /api/challenge returns same challenge after failed POST" $ do
+ let expected = LT.unpack $ decodeUtf8 $ encode $ head defaultChallenges
+
+ void $ postJSON "/api/register" alice
+ void $ postJSON ("/api/challenge?token=" <> LBS.toStrict token) "\"y\""
+
+ get ("/api/challenge?token=" <> LBS.toStrict token)
+ `shouldRespondWith` fromString expected
+
postJSON :: BS.ByteString -> ByteString -> WaiSession () SResponse
postJSON url = request "POST" url [("Content-Type", "application/json")]
@@ -116,7 +130,7 @@ arrayOfLengthOf _ len = MatchBody $ \_ actual ->
app :: StdGen -> IO Application
app g = do
- env <- makeEnv g
+ env <- makeEnv g defaultChallenges
pure $ serveAPI env
testSeed :: StdGen