{-# OPTIONS -Wno-ambiguous-fields #-}
module Test.Presence where
import API.Common
import API.Gundeck
import API.GundeckInternal
import SetupHelpers
import Testlib.Prelude
ensurePresent :: (HasCallStack, MakesValue u) => u -> Int -> App ()
ensurePresent :: forall u. (HasCallStack, MakesValue u) => u -> Int -> App ()
ensurePresent u
u Int
n = App () -> App ()
forall a. App a -> App a
retryT (App () -> App ()) -> App () -> App ()
forall a b. (a -> b) -> a -> b
$ do
ps <- u -> App Response
forall user.
(HasCallStack, MakesValue user) =>
user -> App Response
getPresence u
u App Response -> (Response -> App Value) -> App Value
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Int -> Response -> App Value
Int -> Response -> App Value
getJSON Int
200 App Value -> (Value -> App [Value]) -> App [Value]
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App [Value]
forall a. (HasCallStack, MakesValue a) => a -> App [Value]
asList
length ps `shouldMatchInt` n
registerUser :: (HasCallStack) => App (Value, String)
registerUser :: HasCallStack => App (Value, String)
registerUser = do
alice <- Domain -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App Value
randomUserId Domain
OwnDomain
c <- randomClientId
withWebSocket (alice, "conn", c) $ \WebSocket
_ ->
Value -> Int -> App ()
forall u. (HasCallStack, MakesValue u) => u -> Int -> App ()
ensurePresent Value
alice Int
1
pure (alice, c)
testAddUser :: (HasCallStack) => App ()
testAddUser :: HasCallStack => App ()
testAddUser = App (Value, String) -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void App (Value, String)
HasCallStack => App (Value, String)
registerUser
testRemoveUser :: (HasCallStack) => App ()
testRemoveUser :: HasCallStack => App ()
testRemoveUser = do
(alice, c) <- App (Value, String)
HasCallStack => App (Value, String)
registerUser
void $ generateAndPostPushToken alice c def >>= getJSON 201
do
t <- getPushTokens alice >>= getJSON 200
tokens <- t %. "tokens" & asList
length tokens `shouldMatchInt` 1
do
r <- recipient alice
let push =
[Pair] -> Value
object
[ String
"recipients" String -> [Value] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Value
r],
String
"payload" String -> [Value] -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [[Pair] -> Value
object [String
"foo" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"bar"]]
]
void $ postPush alice [push] >>= getBody 200
void $ unregisterUser alice >>= getBody 200
do
t <- getPushTokens alice >>= getJSON 200
t %. "tokens" `shouldMatch` ([] :: [Value])
do
ns <- getNotifications alice def {client = Just c} >>= getJSON 200
ns %. "notifications" `shouldMatch` ([] :: [Value])
ns %. "has_more" `shouldMatch` False