From d34b146702476f46bcca7d362e56f46227863f1b Mon Sep 17 00:00:00 2001 From: William Carroll Date: Sat, 8 Aug 2020 13:44:22 +0100 Subject: Tests valid and invalid JWTs for the "aud" field Test that when the JWT contains the client ID for my Google app, the JWT is valid, and when it doesn't, it's invalid. --- .../sandbox/learnpianochords/src/server/Spec.hs | 30 ++++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'website/sandbox/learnpianochords/src/server/Spec.hs') diff --git a/website/sandbox/learnpianochords/src/server/Spec.hs b/website/sandbox/learnpianochords/src/server/Spec.hs index 1f9b9bb4bf9c..6c683cbbf2a7 100644 --- a/website/sandbox/learnpianochords/src/server/Spec.hs +++ b/website/sandbox/learnpianochords/src/server/Spec.hs @@ -3,27 +3,29 @@ module Spec where -------------------------------------------------------------------------------- import Test.Hspec -import Web.JWT import Utils +import GoogleSignIn (ValidationResult(..)) import qualified GoogleSignIn import qualified Fixtures as F +import qualified TestUtils -------------------------------------------------------------------------------- main :: IO () main = hspec $ do - describe "GoogleSignIn" $ do + describe "GoogleSignIn" $ describe "jwtIsValid" $ do - it "returns false when the signature is invalid" $ do - let mJWT = F.defaultJWTFields { F.overwriteSigner = hmacSecret "wrong" } - |> F.googleJWT - case mJWT of - Nothing -> True `shouldBe` False - Just jwt -> GoogleSignIn.jwtIsValid jwt `shouldReturn` False + let jwtIsValid' = GoogleSignIn.jwtIsValid True + it "returns validation error when the aud field doesn't match my client ID" $ do + let auds = ["wrong-client-id"] + |> fmap TestUtils.unsafeStringOrURI + encodedJWT = F.defaultJWTFields { F.overwriteAuds = auds } + |> F.googleJWT + jwtIsValid' encodedJWT `shouldReturn` NoMatchingClientIDs auds - it "returns false when the aud field doesn't match my client ID" $ do - let mJWT = F.defaultJWTFields { F.overwriteAud = stringOrURI "wrong" } - |> F.googleJWT - case mJWT of - Nothing -> True `shouldBe` False - Just jwt -> GoogleSignIn.jwtIsValid jwt `shouldReturn` False + it "returns validation success when one of the aud fields matches my client ID" $ do + let auds = ["wrong-client-id", "771151720060-buofllhed98fgt0j22locma05e7rpngl.apps.googleusercontent.com"] + |> fmap TestUtils.unsafeStringOrURI + encodedJWT = F.defaultJWTFields { F.overwriteAuds = auds } + |> F.googleJWT + jwtIsValid' encodedJWT `shouldReturn` Valid -- cgit 1.4.1