module Test.TOTP.Routes where
import qualified API.Brig as Brig
import SetupHelpers
import Test.TOTP.Helpers (isRecoveryCodeShaped, someCredentialId)
import Test.TOTP.MockBodies
import Testlib.Prelude
testTotpActivate :: (HasCallStack) => App ()
testTotpActivate :: HasCallStack => App ()
testTotpActivate = 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
cid <- someCredentialId alice
bindResponse (Brig.activateTotp alice cid mockActivateBody) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
codes <- Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"codes" App Value -> (App Value -> App [Value]) -> App [Value]
forall a b. a -> (a -> b) -> b
& App Value -> App [Value]
forall a. (HasCallStack, MakesValue a) => a -> App [Value]
asList
for_ codes $ \Value
c -> do
code <- Value -> App String
forall user. (HasCallStack, MakesValue user) => user -> App String
asString Value
c
assertBool
("recovery code has the wrong shape: " <> code)
(isRecoveryCodeShaped code)
testTotpList :: (HasCallStack) => App ()
testTotpList :: HasCallStack => App ()
testTotpList = 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
bindResponse (Brig.listTotp alice) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
creds <- Maybe Value -> App [Value]
forall a. (HasCallStack, MakesValue a) => a -> App [Value]
asList Response
resp.json
for_ creds $ \Value
c -> do
App String -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App String -> App ()) -> App String -> App ()
forall a b. (a -> b) -> a -> b
$ App Value -> App String
forall user. (HasCallStack, MakesValue user) => user -> App String
asString (Value
c Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"id")
App String -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App String -> App ()) -> App String -> App ()
forall a b. (a -> b) -> a -> b
$ App Value -> App String
forall user. (HasCallStack, MakesValue user) => user -> App String
asString (Value
c Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"name")
App String -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App String -> App ()) -> App String -> App ()
forall a b. (a -> b) -> a -> b
$ App Value -> App String
forall user. (HasCallStack, MakesValue user) => user -> App String
asString (Value
c Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"created_at")
Value -> String -> App (Maybe Value)
forall a.
(HasCallStack, MakesValue a) =>
a -> String -> App (Maybe Value)
lookupField Value
c String
"secret" App (Maybe Value) -> Maybe Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Maybe Value
noValue
Value -> String -> App (Maybe Value)
forall a.
(HasCallStack, MakesValue a) =>
a -> String -> App (Maybe Value)
lookupField Value
c String
"uri" App (Maybe Value) -> Maybe Value -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` Maybe Value
noValue
testTotpRename :: (HasCallStack) => App ()
testTotpRename :: HasCallStack => App ()
testTotpRename = 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
cid <- someCredentialId alice
Brig.renameTotp alice cid mockRenameBody >>= assertStatus 200
testTotpDelete :: (HasCallStack) => App ()
testTotpDelete :: HasCallStack => App ()
testTotpDelete = 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
cid <- someCredentialId alice
Brig.deleteTotp alice cid mockDeleteBody >>= assertStatus 200