-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2026 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Test.TOTP.OtherRequestBodies where

import qualified API.Brig as Brig
import API.Common (defPassword)
import SetupHelpers
import Test.TOTP.MockBodies
import Testlib.Prelude

-- One endpoint stands in for all of them.  Which bodies accept a totp_code,
-- and that a malformed one is rejected, is settled by the schemas and asserted
-- per type in the unit test (@Test.Wire.API.User.Auth.TOTP@); what is left for
-- an integration test is that a handler which already authenticates the caller
-- neither chokes on the new field nor lets it replace the password.

testMockTotpChangePassword :: (HasCallStack) => App ()
testMockTotpChangePassword :: HasCallStack => App ()
testMockTotpChangePassword =
  ServiceOverrides -> (HasCallStack => String -> App ()) -> App ()
forall a.
HasCallStack =>
ServiceOverrides -> (HasCallStack => String -> App a) -> App a
withModifiedBackend
    ServiceOverrides
forall a. Default a => a
def
      { brigCfg =
          -- Password changes and the final login share the password-hashing rate limit.
          setField "optSettings.setPasswordHashingRateLimit.userLimit.inverseRate" (0 :: Int)
      }
    ((HasCallStack => String -> App ()) -> App ())
-> (HasCallStack => String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
domain -> do
      alice <- String -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser String
domain CreateUser
forall a. Default a => a
def
      email <- asString (alice %. "email")
      let newPassword = String
"new-test-password"
          body =
            [Pair] -> Value
object
              [ String
"old_password" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
defPassword,
                String
"new_password" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
newPassword,
                String
"totp_code" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
mockLoginTotpCode
              ]
          request = Value -> Value -> App Response
forall user.
(HasCallStack, MakesValue user) =>
user -> Value -> App Response
Brig.putPasswordWithBody Value
alice
      -- Do not accept a 4xx from business logic as proof that parsing worked:
      -- these must all fail with 400 before a valid request can mutate the state.
      for_ [toJSON ("123456" :: String), toJSON ("totp:abc" :: String), toJSON ("recovery:invalid" :: String), Number 123456, Bool True] $ \Value
code ->
        String -> Value -> Value -> App Value
forall a b.
(HasCallStack, MakesValue a, ToJSON b) =>
String -> b -> a -> App Value
setField String
"totp_code" Value
code Value
body App Value -> (Value -> App Response) -> App Response
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App Response
request App Response -> (Response -> App ()) -> App ()
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Int -> Response -> App ()
Int -> Response -> App ()
assertStatus Int
400
      setField "old_password" ("wrong-password" :: String) body >>= request >>= assertStatus 403
      request body >>= assertStatus 200
      Brig.login alice email newPassword >>= assertStatus 200