{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Test.FeatureFlags where
import qualified API.Galley as Public
import qualified API.GalleyInternal as Internal
import qualified Data.Aeson as A
import qualified Data.Aeson.Key as A
import qualified Data.Aeson.KeyMap as KM
import qualified Data.Set as Set
import SetupHelpers
import Test.FeatureFlags.Util
import Testlib.Prelude
testLimitedEventFanout :: (HasCallStack) => App ()
testLimitedEventFanout :: HasCallStack => App ()
testLimitedEventFanout = do
let featureName :: String
featureName = String
"limitedEventFanout"
(_alice, team, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
1
bindResponse (Internal.getTeamFeature OwnDomain team featureName) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"status" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"disabled"
assertSuccess =<< Internal.setTeamFeatureStatus OwnDomain team featureName "enabled"
bindResponse (Internal.getTeamFeature OwnDomain team featureName) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json App Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"status" App Value -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"enabled"
testAllFeatures :: (HasCallStack) => App ()
testAllFeatures :: HasCallStack => App ()
testAllFeatures = do
(_, tid, m : _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
2
bindResponse (Public.getTeamFeatures m tid) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json App Value -> Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Value
defAllFeatures
Internal.setTeamFeatureConfig OwnDomain tid "conversationGuestLinks" enabled >>= assertSuccess
bindResponse (Public.getTeamFeatures m tid) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json App Value -> Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Value
defAllFeatures
bindResponse (Public.getFeatureConfigs m) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json App Value -> Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Value
defAllFeatures
randomPersonalUser <- randomUser OwnDomain def
bindResponse (Public.getFeatureConfigs randomPersonalUser) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
Response
resp.json App Value -> Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Value
defAllFeatures
testFeatureConfigConsistency :: (HasCallStack) => App ()
testFeatureConfigConsistency :: HasCallStack => App ()
testFeatureConfigConsistency = do
(_, tid, m : _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
2
allFeaturesRes <- Public.getFeatureConfigs m >>= parseObjectKeys
allTeamFeaturesRes <- Public.getTeamFeatures m tid >>= parseObjectKeys
unless (allTeamFeaturesRes `Set.isSubsetOf` allFeaturesRes)
$ assertFailure (show allTeamFeaturesRes <> " is not a subset of " <> show allFeaturesRes)
where
parseObjectKeys :: Response -> App (Set.Set String)
parseObjectKeys :: Response -> App (Set String)
parseObjectKeys Response
res = do
val <- Response
res.json
case val of
(A.Object Object
hm) -> Set String -> App (Set String)
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([String] -> Set String
forall a. Ord a => [a] -> Set a
Set.fromList ([String] -> Set String)
-> (Object -> [String]) -> Object -> Set String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Key -> String) -> [Key] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> String
forall a. Show a => a -> String
show (Text -> String) -> (Key -> Text) -> Key -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Text
A.toText) ([Key] -> [String]) -> (Object -> [Key]) -> Object -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Object -> [Key]
forall v. KeyMap v -> [Key]
KM.keys (Object -> Set String) -> Object -> Set String
forall a b. (a -> b) -> a -> b
$ Object
hm)
Value
x -> String -> App (Set String)
forall a. HasCallStack => String -> App a
assertFailure (String
"JSON was not an object, but " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value -> String
forall a. Show a => a -> String
show Value
x)
testNonMemberAccess :: (HasCallStack) => Feature -> App ()
testNonMemberAccess :: HasCallStack => Feature -> App ()
testNonMemberAccess (Feature String
featureName) = do
(_, tid, _) <- Domain -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam Domain
OwnDomain Int
0
nonMember <- randomUser OwnDomain def
Public.getTeamFeature nonMember tid featureName
>>= assertForbidden