From e6d000430619613a07974f601e7c132cd12556e9 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Wed, 5 Nov 2025 06:06:22 +0100 Subject: handle post challenge error cases --- lambda-calcul/haskell/test/Minilang/ServerSpec.hs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lambda-calcul/haskell') diff --git a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs index 544d0b7..d2a95d1 100644 --- a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs +++ b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs @@ -21,10 +21,11 @@ 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 - let token = Base64.encode (fst $ newToken testSeed) + let (token, gen) = first Base64.encode (newToken testSeed) alice = encode $ User "alice" it "GET / returns 'index.html'" $ do @@ -81,6 +82,26 @@ spec = with (app testSeed) $ do postJSON ("/api/challenge?token=" <> LBS.toStrict token) response `shouldRespondWith` 200 + it "POST /api/challenge returns 400 given challenge response is incorrect" $ do + let response = "\"y\"" + void $ postJSON "/api/register" alice + + postJSON ("/api/challenge?token=" <> LBS.toStrict token) response + `shouldRespondWith` 400 + + it "POST /api/challenge returns 400 given user is not registered" $ do + let response = "\"x\"" + + postJSON ("/api/challenge?token=" <> LBS.toStrict token) response + `shouldRespondWith` 400 + + it "POST /api/challenge returns 400 given wrong token" $ do + let response = "\"x\"" + otherToken = Base64.encode $ fst $ newToken gen + + postJSON ("/api/challenge?token=" <> LBS.toStrict otherToken) response + `shouldRespondWith` 400 + postJSON :: BS.ByteString -> ByteString -> WaiSession () SResponse postJSON url = request "POST" url [("Content-Type", "application/json")] -- cgit v1.3.1