summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/test/Minilang
diff options
context:
space:
mode:
Diffstat (limited to 'lambda-calcul/haskell/test/Minilang')
-rw-r--r--lambda-calcul/haskell/test/Minilang/ServerSpec.hs49
1 files changed, 48 insertions, 1 deletions
diff --git a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs
index fdf1389..e109977 100644
--- a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs
+++ b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs
@@ -15,7 +15,7 @@ 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 (..), defaultChallenges, newToken)
+import Minilang.API (Challenge (..), Score (..), User (..), defaultChallenges, newToken)
import Minilang.App (makeEnv)
import Minilang.Server (serveAPI)
import Network.Wai (Application)
@@ -121,6 +121,53 @@ spec = with (app testSeed) $ do
get ("/api/challenge?token=" <> LBS.toStrict token)
`shouldRespondWith` fromString expected
+ it "GET /api/score returns user current score" $ do
+ void $ postJSON "/api/register" alice
+ get ("/api/score?token=" <> LBS.toStrict token)
+ `shouldRespondWith` 200
+ { matchBody = MatchBody $ \_ actual ->
+ case eitherDecode actual of
+ Right (score :: Score) ->
+ if score == Score {user = User "alice", grade = 0, lastTest = Nothing, lastResult = Nothing}
+ then Nothing
+ else Just $ "Expected score with grade 0, but got " ++ show score
+ Left err -> Just $ "Failed to decode JSON: " ++ err
+ }
+
+ it "GET /api/score returns 400 given token is not registered" $ do
+ get ("/api/score?token=" <> LBS.toStrict token)
+ `shouldRespondWith` 400
+
+ it "GET /api/score returns score with success given last challenge succeeded" $ do
+ void $ postJSON "/api/register" alice
+ void $ postJSON ("/api/challenge?token=" <> LBS.toStrict token) "\"x\""
+
+ get ("/api/score?token=" <> LBS.toStrict token)
+ `shouldRespondWith` 200
+ { matchBody = MatchBody $ \_ actual ->
+ case eitherDecode actual of
+ Right (score :: Score) ->
+ if lastResult score == Just True
+ then Nothing
+ else Just $ "Expected score with success, but got " ++ show score
+ Left err -> Just $ "Failed to decode JSON: " ++ err
+ }
+
+ it "GET /api/score returns score with failure given last challenge failed" $ do
+ void $ postJSON "/api/register" alice
+ void $ postJSON ("/api/challenge?token=" <> LBS.toStrict token) "\"y\""
+
+ get ("/api/score?token=" <> LBS.toStrict token)
+ `shouldRespondWith` 200
+ { matchBody = MatchBody $ \_ actual ->
+ case eitherDecode actual of
+ Right (score :: Score) ->
+ if lastResult score == Just False
+ then Nothing
+ else Just $ "Expected score with failure, but got " ++ show score
+ Left err -> Just $ "Failed to decode JSON: " ++ err
+ }
+
postJSON :: BS.ByteString -> ByteString -> WaiSession () SResponse
postJSON url = request "POST" url [("Content-Type", "application/json")]