module Test.Migration.Nonce (testNonceMigration) where
import API.Brig
import qualified API.BrigInternal as BrigInternal
import API.Common
import Control.Monad.Codensity
import Control.Monad.Reader
import qualified Data.ByteString.Char8 as BC
import qualified Data.CaseInsensitive as CI
import SetupHelpers (createTeam)
import Test.Migration.Util (waitForMigration)
import Testlib.Prelude
import Testlib.ResourcePool
testNonceMigration :: (HasCallStack) => App ()
testNonceMigration :: HasCallStack => App ()
testNonceMigration = do
resourcePool <- (Env -> ResourcePool BackendResource)
-> App (ResourcePool BackendResource)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (.resourcePool)
runCodensity (acquireResources 1 resourcePool) $ \[BackendResource
backend] -> do
let domain :: String
domain = BackendResource
backend.berDomain
checkNonceRoundtrip :: App ()
checkNonceRoundtrip = do
(owner, _tid, _members) <- String -> Int -> App (Value, String, [Value])
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> Int -> App (Value, String, [Value])
createTeam String
domain Int
1
assertSuccess =<< putSelf owner (PutSelf Nothing Nothing (Just "nonce-owner") Nothing)
handle <- randomHandle
assertSuccess =<< putHandle owner handle
client <- bindResponse (addClient owner def) $ getJSON 201
cid <- client %. "id" & asString
nonceResp <- BrigInternal.newNonce owner cid
assertStatus 204 nonceResp
assertBool "missing Replay-Nonce header"
$ isJust (findHeader (CI.mk (BC.pack "Replay-Nonce")) nonceResp)
assertStatus 400 =<< BrigInternal.createDPoPAccessToken owner cid
replayResp <- BrigInternal.createDPoPAccessToken owner cid
assertStatus 400 replayResp
replayResp.json %. "label" `shouldMatch` "client-token-bad-nonce"
Codensity App String -> forall b. (String -> App b) -> App b
forall k (m :: k -> *) a.
Codensity m a -> forall (b :: k). (a -> m b) -> m b
runCodensity (HasCallStack =>
BackendResource -> ServiceOverrides -> Codensity App String
BackendResource -> ServiceOverrides -> Codensity App String
startDynamicBackend BackendResource
backend (String -> Bool -> ServiceOverrides
conf String
"cassandra" Bool
False)) ((String -> App ()) -> App ()) -> (String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ App () -> String -> App ()
forall a b. a -> b -> a
const App ()
checkNonceRoundtrip
Codensity App String -> forall b. (String -> App b) -> App b
forall k (m :: k -> *) a.
Codensity m a -> forall (b :: k). (a -> m b) -> m b
runCodensity (HasCallStack =>
BackendResource -> ServiceOverrides -> Codensity App String
BackendResource -> ServiceOverrides -> Codensity App String
startDynamicBackend BackendResource
backend (String -> Bool -> ServiceOverrides
conf String
"migration-to-postgresql" Bool
True)) ((String -> App ()) -> App ()) -> (String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ \String
_ -> do
HasCallStack => String -> String -> App ()
String -> String -> App ()
waitForMigration String
domain String
counterName
App ()
checkNonceRoundtrip
Codensity App String -> forall b. (String -> App b) -> App b
forall k (m :: k -> *) a.
Codensity m a -> forall (b :: k). (a -> m b) -> m b
runCodensity (HasCallStack =>
BackendResource -> ServiceOverrides -> Codensity App String
BackendResource -> ServiceOverrides -> Codensity App String
startDynamicBackend BackendResource
backend (String -> Bool -> ServiceOverrides
conf String
"postgresql" Bool
False)) ((String -> App ()) -> App ()) -> (String -> App ()) -> App ()
forall a b. (a -> b) -> a -> b
$ App () -> String -> App ()
forall a b. a -> b -> a
const App ()
checkNonceRoundtrip
where
conf :: String -> Bool -> ServiceOverrides
conf :: String -> Bool -> ServiceOverrides
conf String
db Bool
runMigration =
ServiceOverrides
forall a. Default a => a
def
{ brigCfg = setField "postgresMigration.nonce" db,
backgroundWorkerCfg =
setField "postgresMigration.nonce" db
>=> setField "migrateNonce" runMigration
}
counterName :: String
counterName :: String
counterName = String
"^wire_nonce_migration_finished"