-- 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.Nginz where

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

testTotpThroughNginz :: (HasCallStack) => App ()
testTotpThroughNginz :: HasCallStack => App ()
testTotpThroughNginz = do
  alice <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
  email <- asString (alice %. "email")
  token <- do
    resp <- Brig.login alice email defPassword >>= getJSON 200
    asString (resp %. "access_token")

  -- authenticated: the request reaches brig
  do
    req <-
      rawBaseRequest alice Nginz Versioned "/self/totp"
        <&> addHeader "Authorization" ("Bearer " <> token)
    bindResponse (submit "GET" req) $ \Response
resp -> do
      Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
      App [Value] -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App [Value] -> App ()) -> App [Value] -> App ()
forall a b. (a -> b) -> a -> b
$ Maybe Value -> App [Value]
forall a. (HasCallStack, MakesValue a) => a -> App [Value]
asList Response
resp.json

  -- a bad token: nginz rejects it before brig sees anything
  do
    req <-
      rawBaseRequest alice Nginz Versioned "/self/totp"
        <&> addHeader "Authorization" "Bearer not-a-token"
    submit "GET" req >>= assertStatus 401

  -- no credentials at all
  do
    req <- rawBaseRequest alice Nginz Versioned "/self/totp"
    bindResponse (submit "GET" req) $ \Response
resp ->
      HasCallStack => String -> Bool -> App ()
String -> Bool -> App ()
assertBool
        (String
"unauthenticated request was not rejected: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Response
resp.status)
        (Response
resp.status Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
400 Bool -> Bool -> Bool
&& Response
resp.status Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
500)