-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2025 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.Cargohold.AssetUpload where

import API.Cargohold
import qualified Data.ByteString.Char8 as BSC
import SetupHelpers
import Testlib.Prelude

testAssetUploadVerifiedUser :: (HasCallStack) => App ()
testAssetUploadVerifiedUser :: HasCallStack => App ()
testAssetUploadVerifiedUser = do
  user <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
  bindResponse (uploadSomeAsset user) $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201

testAssetUploadUnknownUser :: (HasCallStack) => App ()
testAssetUploadUnknownUser :: HasCallStack => App ()
testAssetUploadUnknownUser = do
  uid <- App String
HasCallStack => App String
randomId
  domain <- make OwnDomain
  let user =
        [Pair] -> Value
object
          [ String
"id" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
uid,
            String
"qualified_id"
              String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Pair] -> Value
object
                [ String
"domain" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
domain,
                  String
"id" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
uid
                ]
          ]
  bindResponse (uploadSomeAsset user) $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
403

testUploadAssetEphemeralUser :: (HasCallStack) => App ()
testUploadAssetEphemeralUser :: HasCallStack => App ()
testUploadAssetEphemeralUser = do
  user <- Domain -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App Value
ephemeralUser Domain
OwnDomain

  key <- bindResponse (uploadSomeAsset user) $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201
    Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"key"

  bindResponse (downloadAsset user user key "nginz-https.example.com" id) $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
    ByteString -> String
BSC.unpack Response
resp.body String -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"Hello World!"

testUploadAssetFileSizeLimit :: (HasCallStack) => App ()
testUploadAssetFileSizeLimit :: HasCallStack => App ()
testUploadAssetFileSizeLimit = do
  let restrictUploadLimitOnlyForTeamUser :: ServiceOverrides
restrictUploadLimitOnlyForTeamUser = ServiceOverrides
forall a. Default a => a
def {cargoholdCfg = setField "settings.maxTotalBytes" (1 :: Int)}
  ServiceOverrides -> (HasCallStack => String -> App ()) -> App ()
forall a.
HasCallStack =>
ServiceOverrides -> (HasCallStack => String -> App a) -> App a
withModifiedBackend ServiceOverrides
restrictUploadLimitOnlyForTeamUser ((HasCallStack => String -> App ()) -> App ())
-> (HasCallStack => String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
domain -> do
    (_, _, teamUser : _) <- String -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam String
domain Int
2
    bindResponse (uploadSomeAsset teamUser) $ \Response
resp -> do
      Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
413
      Response
resp.json Maybe 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
"client-error"

    nonTeamUser1 <- ephemeralUser domain
    nonTeamUser2 <- randomUser domain def
    for_ [nonTeamUser1, nonTeamUser2] $ \Value
user ->
      App Response -> (Response -> App ()) -> App ()
forall a.
HasCallStack =>
App Response -> (Response -> App a) -> App a
bindResponse (Value -> App Response
forall user.
(HasCallStack, MakesValue user) =>
user -> App Response
uploadSomeAsset Value
user) ((Response -> App ()) -> App ()) -> (Response -> App ()) -> App ()
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
201

testUploadAssetFileSizeLimitStrict :: (HasCallStack) => App ()
testUploadAssetFileSizeLimitStrict :: HasCallStack => App ()
testUploadAssetFileSizeLimitStrict = do
  let restrictUploadLimitOnlyForNonTeamUser :: ServiceOverrides
restrictUploadLimitOnlyForNonTeamUser = ServiceOverrides
forall a. Default a => a
def {cargoholdCfg = setField "settings.maxTotalBytesStrict" (1 :: Int)}
  ServiceOverrides -> (HasCallStack => String -> App ()) -> App ()
forall a.
HasCallStack =>
ServiceOverrides -> (HasCallStack => String -> App a) -> App a
withModifiedBackend ServiceOverrides
restrictUploadLimitOnlyForNonTeamUser ((HasCallStack => String -> App ()) -> App ())
-> (HasCallStack => String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
domain -> do
    nonTeamUser1 <- String -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App Value
ephemeralUser String
domain
    nonTeamUser2 <- randomUser domain def
    for_ [nonTeamUser1, nonTeamUser2] $ \Value
user ->
      App Response -> (Response -> App ()) -> App ()
forall a.
HasCallStack =>
App Response -> (Response -> App a) -> App a
bindResponse (Value -> App Response
forall user.
(HasCallStack, MakesValue user) =>
user -> App Response
uploadSomeAsset Value
user) ((Response -> App ()) -> App ()) -> (Response -> App ()) -> App ()
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
413
        Response
resp.json Maybe 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
"client-error"

    (_, _, teamUser : _) <- createTeam domain 2
    bindResponse (uploadSomeAsset teamUser) $ \Response
resp -> do
      Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201