module Test.PasswordReset where
import API.Brig
import API.BrigInternal hiding (activate)
import API.Common
import SetupHelpers
import Testlib.Prelude
testPasswordResetShouldSucceedButFailOnWrongInputs :: (HasCallStack) => App ()
testPasswordResetShouldSucceedButFailOnWrongInputs :: HasCallStack => App ()
testPasswordResetShouldSucceedButFailOnWrongInputs = do
let noRateLimitCfg :: ServiceOverrides
noRateLimitCfg =
ServiceOverrides
forall a. Default a => a
def
{ brigCfg =
setField "optSettings.setPasswordHashingRateLimit.userLimit.inverseRate" (0 :: Int)
}
ServiceOverrides -> (HasCallStack => String -> App ()) -> App ()
forall a.
HasCallStack =>
ServiceOverrides -> (HasCallStack => String -> App a) -> App a
withModifiedBackend ServiceOverrides
noRateLimitCfg ((HasCallStack => String -> App ()) -> App ())
-> (HasCallStack => String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
domain -> do
u <- String -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser String
domain CreateUser
forall a. Default a => a
def
email <- u %. "email" & asString
passwordReset u email >>= assertSuccess
passwordReset u email >>= assertSuccess
(key, code) <- getPasswordResetData domain email
let newPassword = String
"newpassword"
completePasswordReset u "wrong-key" code newPassword >>= assertStatus 400
login u email newPassword >>= assertStatus 403
completePasswordReset u key "wrong-code" newPassword >>= assertStatus 400
login u email newPassword >>= assertStatus 403
completePasswordReset u key code newPassword >>= assertSuccess
login u email defPassword >>= assertStatus 403
login u email newPassword >>= assertSuccess
passwordReset u email >>= assertSuccess
(nextKey, nextCode) <- getPasswordResetData domain email
bindResponse (completePasswordReset u nextKey nextCode newPassword) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
409
Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"label" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"password-must-differ"
testPasswordResetAfterEmailUpdate :: (HasCallStack) => App ()
testPasswordResetAfterEmailUpdate :: HasCallStack => App ()
testPasswordResetAfterEmailUpdate = do
u <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
email <- u %. "email" & asString
(cookie, token) <- bindResponse (login u email defPassword) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
token <- Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"access_token" App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
let cookie = Maybe String -> String
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ String -> Response -> Maybe String
getCookie String
"zuid" Response
resp
pure ("zuid=" <> cookie, token)
newEmail <- randomEmail
updateEmail u newEmail cookie token >>= assertSuccess
passwordReset u email >>= assertSuccess
(key, code) <- getPasswordResetData OwnDomain email
bindResponse (getActivationCode u newEmail) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
activationKey <- Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"key" App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
activationCode <- resp.json %. "code" & asString
activate u activationKey activationCode >>= assertSuccess
bindResponse (getSelf u) $ \Response
resp -> do
actualEmail <- Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"email"
actualEmail `shouldMatch` newEmail
bindResponse (completePasswordReset u key code "newpassword") $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
400
Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"label" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"invalid-code"
testPasswordResetInvalidPasswordLength :: App ()
testPasswordResetInvalidPasswordLength :: App ()
testPasswordResetInvalidPasswordLength = do
u <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
email <- u %. "email" & asString
passwordReset u email >>= assertSuccess
(key, code) <- getPasswordResetData OwnDomain email
let shortPassword = String
"123456"
completePasswordReset u key code shortPassword >>= assertStatus 400
login u email shortPassword >>= assertStatus 403
getPasswordResetData :: (HasCallStack, MakesValue domain) => domain -> String -> App (String, String)
getPasswordResetData :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> App (String, String)
getPasswordResetData domain
domain String
email = do
App Response
-> (Response -> App (String, String)) -> App (String, String)
forall a.
HasCallStack =>
App Response -> (Response -> App a) -> App a
bindResponse (domain -> String -> App Response
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> App Response
getPasswordResetCode domain
domain String
email) ((Response -> App (String, String)) -> App (String, String))
-> (Response -> App (String, String)) -> App (String, String)
forall a b. (a -> b) -> a -> b
$ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
(,) (String -> String -> (String, String))
-> App String -> App (String -> (String, String))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"key" App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString) App (String -> (String, String))
-> App String -> App (String, String)
forall a b. App (a -> b) -> App a -> App b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"code" App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString)