-- 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.FeatureFlags.PreventAdminlessGroups where

import qualified API.Galley as Public
import SetupHelpers
import Test.FeatureFlags.Util
import Testlib.Prelude

testPreventAdminlessGroups :: (HasCallStack) => APIAccess -> App ()
testPreventAdminlessGroups :: HasCallStack => APIAccess -> App ()
testPreventAdminlessGroups APIAccess
access =
  String -> FeatureTests
mkFeatureTests String
"preventAdminlessGroups"
    FeatureTests -> (FeatureTests -> FeatureTests) -> FeatureTests
forall a b. a -> (a -> b) -> b
& Value -> FeatureTests -> FeatureTests
addUpdate Value
validConfig
    FeatureTests -> (FeatureTests -> FeatureTests) -> FeatureTests
forall a b. a -> (a -> b) -> b
& Value -> FeatureTests -> FeatureTests
addInvalidUpdate Value
invalidConfig
    FeatureTests -> (FeatureTests -> App ()) -> App ()
forall a b. a -> (a -> b) -> b
& Domain -> APIAccess -> FeatureTests -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> APIAccess -> FeatureTests -> App ()
runFeatureTests Domain
OwnDomain APIAccess
access

validConfig :: Value
validConfig :: Value
validConfig =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
canonicalPreventAdminlessGroupsConfig
    ]

invalidConfig :: Value
invalidConfig :: Value
invalidConfig =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config"
        String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Pair] -> Value
object
          [ String
"promotionStrategy" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"dsdfhjsdf",
            String
"deletionTimeout" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
30 :: Int),
            String
"reminderTimeouts" String -> [Int] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= ([Int
15, Int
20, Int
25] :: [Int]),
            String
"deletionTimeoutDuration" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"30d",
            String
"reminderTimeoutDurations" String -> [String] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [String
"15d", String
"20d", String
"25d"]
          ]
    ]

testPatchPreventAdminlessGroups :: (HasCallStack) => App ()
testPatchPreventAdminlessGroups :: HasCallStack => App ()
testPatchPreventAdminlessGroups = do
  Domain -> String -> Value -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> Value -> App ()
checkPatch Domain
OwnDomain String
"preventAdminlessGroups"
    (Value -> App ()) -> Value -> App ()
forall a b. (a -> b) -> a -> b
$ [Pair] -> Value
object [String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"locked"]
  Domain -> String -> Value -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> Value -> App ()
checkPatch Domain
OwnDomain String
"preventAdminlessGroups"
    (Value -> App ()) -> Value -> App ()
forall a b. (a -> b) -> a -> b
$ [Pair] -> Value
object [String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"disabled"]
  Domain -> String -> Value -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> Value -> App ()
checkPatch Domain
OwnDomain String
"preventAdminlessGroups"
    (Value -> App ()) -> Value -> App ()
forall a b. (a -> b) -> a -> b
$ [Pair] -> Value
object [String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"locked", String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"disabled"]
  Domain -> String -> Value -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> Value -> App ()
checkPatch Domain
OwnDomain String
"preventAdminlessGroups"
    (Value -> App ()) -> Value -> App ()
forall a b. (a -> b) -> a -> b
$ [Pair] -> Value
object
      [ String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"unlocked",
        String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
canonicalPreventAdminlessGroupsConfig
      ]

testPreventAdminlessGroupsPutV16AcceptsLegacyTimeoutFields :: (HasCallStack) => App ()
testPreventAdminlessGroupsPutV16AcceptsLegacyTimeoutFields :: HasCallStack => App ()
testPreventAdminlessGroupsPutV16AcceptsLegacyTimeoutFields = do
  (owner, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
  bindResponse
    ( Public.setTeamFeatureConfigVersioned
        (ExplicitVersion 16)
        owner
        tid
        "preventAdminlessGroups"
        legacyTimeoutFeatureConfig
    )
    $ \Response
resp -> do
      Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
      Response
resp.json Maybe Value -> Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Value
canonicalPreventAdminlessGroupsFeature
  checkFeature "preventAdminlessGroups" owner tid canonicalPreventAdminlessGroupsFeature

testPreventAdminlessGroupsPutV17AcceptsDurationTimeoutFields :: (HasCallStack) => App ()
testPreventAdminlessGroupsPutV17AcceptsDurationTimeoutFields :: HasCallStack => App ()
testPreventAdminlessGroupsPutV17AcceptsDurationTimeoutFields = do
  (owner, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
  bindResponse
    ( Public.setTeamFeatureConfigVersioned
        (ExplicitVersion 17)
        owner
        tid
        "preventAdminlessGroups"
        durationTimeoutFeatureConfig
    )
    $ \Response
resp -> do
      Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
      Response
resp.json Maybe Value -> Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Value
canonicalPreventAdminlessGroupsFeature
  checkFeature "preventAdminlessGroups" owner tid canonicalPreventAdminlessGroupsFeature

testPreventAdminlessGroupsPutV16RejectsDurationOnlyTimeoutFields :: (HasCallStack) => App ()
testPreventAdminlessGroupsPutV16RejectsDurationOnlyTimeoutFields :: HasCallStack => App ()
testPreventAdminlessGroupsPutV16RejectsDurationOnlyTimeoutFields = do
  (owner, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
  Public.setTeamFeatureConfigVersioned
    (ExplicitVersion 16)
    owner
    tid
    "preventAdminlessGroups"
    durationTimeoutFeatureConfig
    >>= assertStatus 400

legacyTimeoutFeatureConfig :: Value
legacyTimeoutFeatureConfig :: Value
legacyTimeoutFeatureConfig =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
legacyTimeoutConfig
    ]

durationTimeoutFeatureConfig :: Value
durationTimeoutFeatureConfig :: Value
durationTimeoutFeatureConfig =
  [Pair] -> Value
object
    [ String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
durationTimeoutConfig
    ]

canonicalPreventAdminlessGroupsFeature :: Value
canonicalPreventAdminlessGroupsFeature :: Value
canonicalPreventAdminlessGroupsFeature =
  [Pair] -> Value
object
    [ String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"unlocked",
      String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"enabled",
      String
"ttl" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"unlimited",
      String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
canonicalPreventAdminlessGroupsConfig
    ]

canonicalPreventAdminlessGroupsConfig :: Value
canonicalPreventAdminlessGroupsConfig :: Value
canonicalPreventAdminlessGroupsConfig =
  [Pair] -> Value
object
    [ String
"promotionStrategy" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"random",
      String
"deletionTimeout" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
30 :: Int),
      String
"reminderTimeouts" String -> [Int] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= ([Int
15, Int
20, Int
25] :: [Int]),
      String
"deletionTimeoutDuration" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"30d",
      String
"reminderTimeoutDurations" String -> [String] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [String
"15d", String
"20d", String
"25d"]
    ]

legacyTimeoutConfig :: Value
legacyTimeoutConfig :: Value
legacyTimeoutConfig =
  [Pair] -> Value
object
    [ String
"promotionStrategy" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"random",
      String
"deletionTimeout" String -> Int -> Pair
forall a. ToJSON a => String -> a -> Pair
.= (Int
30 :: Int),
      String
"reminderTimeouts" String -> [Int] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= ([Int
15, Int
20, Int
25] :: [Int])
    ]

durationTimeoutConfig :: Value
durationTimeoutConfig :: Value
durationTimeoutConfig =
  [Pair] -> Value
object
    [ String
"promotionStrategy" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"random",
      String
"deletionTimeoutDuration" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"30d",
      String
"reminderTimeoutDurations" String -> [String] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [String
"15d", String
"20d", String
"25d"]
    ]